JDOM 元素创建

发布于 2024-12-20 22:14:04 字数 1122 浏览 4 评论 0原文

我是 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

不…忘初心 2024-12-27 22:14:04

给定您想要的示例:

<?xml version="1.0" encoding="UTF-8"?>
<article xmlns="http://www.javaworld.com">
  <title>Blah, blah, blah</title>
</article>

这意味着

的所有子元素与

具有相同的命名空间,即命名空间从父元素继承到子元素。这意味着您需要为所有子元素指定 jwNS ,即
myElement.addContent(new Element("title", jwNS ).setText("Blah, blah, blah"));

在呈现 XML 输出时,JDOM 应省略 </code> 中的显式命名空间,因为它继承自 <code><article></code>。

通过仅使用 new Element("title"),您就表示您不希望 </code> 上有命名空间,因此 JDOm 必须添加显式 <code> >xnmns=""</code> 属性,以便覆盖从 <code><article></code> 父级继承 <code>jwNS</code> 命名空间。

Given your desired example:

<?xml version="1.0" encoding="UTF-8"?>
<article xmlns="http://www.javaworld.com">
  <title>Blah, blah, blah</title>
</article>

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 specify jwNS for all of your child elements, i.e.

myElement.addContent(new Element("title", jwNS ).setText("Blah, blah, blah"));

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 explicit xnmns="" attribute in order to override the inheritance of the jwNS namespace from the <article> parent.

怀里藏娇 2024-12-27 22:14:04

尝试使用以下方式创建元素:

Element myElement = new Element("article");

而不是

Element myElement = new Element("article", jwNS);

Try creating your element by using:

Element myElement = new Element("article");

instead of

Element myElement = new Element("article", jwNS);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文