JDOM 元素创建
我是 JDOM 新手,在创建文档时遇到问题。问题是我想要 能够添加不具有“xmlns”属性的元素。我正在使用 JDOM 1.1
我发现的所有示例都显示输出没有“xmlns”。这是一个简单的 代码片段及其输出:
Namespace jwNS = Namespace.getNamespace("http://www.javaworld.com");
Element myElement = new Element("article", jwNS);
Document doc = new Document(myElement);
myElement.addContent(new Element("title").setText("Blah, blah, blah"));
// serialize with two space indents and extra line breaks
try {
//XMLOutputter serializer = new XMLOutputter(" ", true);
XMLOutputter serializer = new XMLOutputter(Format.getPrettyFormat());
serializer.output(doc, System.out);
}
catch (IOException e) {
System.err.println(e);
}
输出:
<?xml version="1.0" encoding="UTF-8"?>
<article xmlns="http://www.javaworld.com">
<title xmlns="">Blah, blah, blah</title>
</article>
我想要的只是拥有
<?xml version="1.0" encoding="UTF-8"?>
<article xmlns="http://www.javaworld.com">
<title>Blah, blah, blah</title>
</article>
谁能告诉我我做错了什么?
I am new to JDOM, and am having trouble creating a document. The problem is that I want
to be able to add Elements that do NOT have the "xmlns" attribute. I am using JDOM 1.1
All of the examples I have found show the output without the "xmlns". Here is a simple
code fragment, along with its output:
Namespace jwNS = Namespace.getNamespace("http://www.javaworld.com");
Element myElement = new Element("article", jwNS);
Document doc = new Document(myElement);
myElement.addContent(new Element("title").setText("Blah, blah, blah"));
// serialize with two space indents and extra line breaks
try {
//XMLOutputter serializer = new XMLOutputter(" ", true);
XMLOutputter serializer = new XMLOutputter(Format.getPrettyFormat());
serializer.output(doc, System.out);
}
catch (IOException e) {
System.err.println(e);
}
Output:
<?xml version="1.0" encoding="UTF-8"?>
<article xmlns="http://www.javaworld.com">
<title xmlns="">Blah, blah, blah</title>
</article>
What I want is to just have
<?xml version="1.0" encoding="UTF-8"?>
<article xmlns="http://www.javaworld.com">
<title>Blah, blah, blah</title>
</article>
Can anyone tell me what I am doing wrong?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
给定您想要的示例:
这意味着
的所有子元素与
具有相同的命名空间,即命名空间从父元素继承到子元素。这意味着您需要为所有子元素指定
jwNS
,即在呈现 XML 输出时,JDOM 应省略</code> 中的显式命名空间,因为它继承自 <code><article></code>。
通过仅使用</code> 上有命名空间,因此 JDOm 必须添加显式 <code> >xnmns=""</code> 属性,以便覆盖从 <code><article></code> 父级继承 <code>jwNS</code> 命名空间。
new Element("title")
,您就表示您不希望Given your desired example:
This means that all child elements of
<article>
have the same namespace as<article>
, i.e. namespaces are inherited from parents to children. That means you need to specifyjwNS
for all of your child elements, i.e.When rendering the XML output, JDOM should then omit the explicit namespace from
<title>
, since it inherits it from<article>
.By using just
new Element("title")
, you're saying that you want no namespace on<title>
, and so JDOm has to add an explicitxnmns=""
attribute in order to override the inheritance of thejwNS
namespace from the<article>
parent.尝试使用以下方式创建元素:
而不是
Try creating your element by using:
instead of