开发者

inserting xml:space='preserve' in the DOM

开发者 https://www.devze.com 2023-01-10 18:38 出处:网络
Java, Xerces 2.9.1 insertHere.setAttributeNS(XMLConstants.XML_NS_URI, \"xml:space\", \"preserve\"); and

Java, Xerces 2.9.1

insertHere.setAttributeNS(XMLConstants.XML_NS_URI, "xml:space", "preserve");

and

insertHere.setAttributeNS(XMLConstants.XML_NS_URI, "space", "preserve")

both end up with an attribute of just space='preserve', no XML prefix.

insertHere.setAttribute( "xml:space", "preserve")

works, but it seems somehow wrong. Am I missing anything?

EDIT

I checked.

I read a template document in with setNamespaceAware turned on.

开发者_如何学Go

I then use the following to make a copy of it, and then I start inserting new elements.

 public static Document copyDocument(Document input) {
        DocumentType oldDocType = input.getDoctype();
        DocumentType newDocType = null;
        Document newDoc;
        String oldNamespaceUri = input.getDocumentElement().getNamespaceURI();
        if (oldDocType != null) {
            // cloning doctypes is 'implementation dependent'
            String oldDocTypeName = oldDocType.getName();
            newDocType = input.getImplementation().createDocumentType(oldDocTypeName,
                                                                      oldDocType.getPublicId(),
                                                                      oldDocType.getSystemId());
            newDoc = input.getImplementation().createDocument(oldNamespaceUri, oldDocTypeName,
                                                              newDocType);
        } else {
            newDoc = input.getImplementation().createDocument(oldNamespaceUri,
                                                              input.getDocumentElement().getNodeName(),
                                                              null);
        }
        Element newDocElement = (Element)newDoc.importNode(input.getDocumentElement(), true);
        newDoc.replaceChild(newDocElement, newDoc.getDocumentElement());
        return newDoc;
    }


When I run the following code:

import javax.xml.XMLConstants;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;

import org.w3c.dom.Document;
import org.w3c.dom.Element;

public class Demo {

    public static void main(String[] args) throws Exception {
        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
        dbf.setNamespaceAware(true);
        DocumentBuilder db = dbf.newDocumentBuilder();
        Document document = db.newDocument();

        Element rootElement = document.createElement("root");
        document.appendChild(rootElement);
        rootElement.setAttributeNS(XMLConstants.XML_NS_URI, "space", "preserve");

        TransformerFactory tf = TransformerFactory.newInstance();
        Transformer t = tf.newTransformer();
        t.transform(new DOMSource(document), new StreamResult(System.out));
    }

}

I get the following output:

<root xml:space="preserve"/>

How are you building your document?

0

精彩评论

暂无评论...
验证码 换一张
取 消