How to create XML with declared namespace?

左心房为你撑大大i 提交于 2020-01-16 03:25:29

问题


I am creating a xml request using java. I am new in creating xmls using java.

Here is code:

Document doc = docBuilder.newDocument();
            Element rootElement = doc.createElement("UserRequest");
            rootElement.setAttributeNS("http://www.w3.org/2000/xmlns/", "xmlns:ns0", "https://com.user.req");
            rootElement.setAttributeNS("http://www.w3.org/2000/xmlns/", "xmlns:xsi", "http://www.w3.org/2001/XMLSchema-instance");
            doc.appendChild(rootElement);

            // user element
            Element user = doc.createElement("User");
            rootElement.appendChild(user);

            // userAttributes element
            Element userAttr = doc.createElement("UserAttributes");
            rootElement.appendChild(userAttr);


            // name elements
            Element name = doc.createElement("Name");
            name.appendChild(doc.createTextNode("hello"));
            userAttr.appendChild(name);
            // value elements
            Element value = doc.createElement("Value");
            name.appendChild(doc.createTextNode("dude"));
            userAttr.appendChild(value);

Expected output:

<?xml version="1.0" encoding="UTF-8"?>
<UserRequest 
xmlns:ns0="https://com.user.req" 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xsi:type="ns0:UserRequest">
  <User/>
  <UserAttributes>
    <Name>hello</Name>
    <Value>dude</Value>
  </UserAttributes>
</UserRequest>

Generated output:

<?xml version="1.0" encoding="UTF-8"?>
<UserRequest 
xmlns:ns0="https://com.user.req" 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 

  <User/>
  <UserAttributes>
    <Name>hello</Name>
    <Value>dude</Value>
  </UserAttributes>
</UserRequest>

How to get correct namespace (as shown at expected section).


回答1:


There's nothing wrong with the namespaces in your generated output. However this is an accident ... you're using setAttributeNS() to do something it's not intended for.

Read up on XML namespace declarations and namespace prefixes. That will be a lot easier than trying to explain point-by-point why you're not getting what you expected. For example, xmlns is not a namespace prefix, and xsi:type is not a namespace.

Instead of trying to create the desired namespace declarations as if they were normal attributes, delete these two lines

rootElement.setAttributeNS("http://www.w3.org/2000/xmlns/",
        "xmlns:ns0", "https://com.user.req");
rootElement.setAttributeNS("http://www.w3.org/2000/xmlns/",
        "xmlns:xsi", "http://www.w3.org/2001/XMLSchema-instance");

and instead use

rootElement.setAttributeNS("http://www.w3.org/2001/XMLSchema-instance",
        "xsi:type", "ns0:UserRequest");

This should give you most of your expected output, except for the ns0 namespace prefix declaration. It won't generate that because you're not using ns0 on any element or attribute. Did you mean to have

<ns0:UserRequest ...

in your expected output?



来源:https://stackoverflow.com/questions/34862775/how-to-create-xml-with-declared-namespace

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!