如何使用声明的命名空间创建 XML? [英] How to create XML with declared namespace?

查看:51
本文介绍了如何使用声明的命名空间创建 XML?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 java 创建一个 xml 请求.我是使用 java 创建 xml 的新手.

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

代码如下:

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);

预期输出:

<?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>

生成的输出:

<?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>

如何获得正确的命名空间(如预期部分所示).

推荐答案

生成的输出中的命名空间没有任何问题.然而这是一个意外......你正在使用 setAttributeNS() 做一些它不打算做的事情.

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.

阅读 XML 命名空间声明和命名空间前缀.这比逐点解释为什么没有得到预期要容易得多.例如,xmlns 不是命名空间前缀,xsi:type 不是命名空间.

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");

改为使用

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

除了 ns0 命名空间前缀声明之外,这应该会为您提供大部分预期输出.它不会生成它,因为您没有在任何元素或属性上使用 ns0.你的意思是

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 ...

在您的预期输出中?

这篇关于如何使用声明的命名空间创建 XML?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆