如何使用 XOM 仅在第一个标签上设置命名空间?

发布于 2024-09-08 22:59:19 字数 1165 浏览 1 评论 0原文

我正在使用 XOM 在 Java 中构建 XML 文档。

我创建了一个简单的 XML 文档,并且需要一个 XML 命名空间。但是,当我在第一个标签上设置命名空间时,会在子级上设置一个空命名空间,例如 xmlns="",我怎样才能摆脱这种行为?我只想在第一个标签上使用 xmlns

我想要这个 XML:

<request xmlns="http://my-namespace">
    <type>Test</type>
    <data>
        <myData>test data</myData>
    </data>
</request>

但这是 XOM 的 XML 文档输出

<request xmlns="http://my-namespace">
    <type xmlns="">Test</type>
    <data xmlns="">
        <myData>test data</myData>
    </data>
</request>

这是我的 Java XOM 代码:

String namespace = "http://my-namespace";
Element request = new Element("request", namespace);

Element type = new Element("type");
type.appendChild("Test");

request.appendChild(type);

Element data = new Element("data");
request.appendChild(data);

Element myData = new Element("myData");
myData.appendChild("test data");
data.appendChild(myData);

Document doc = new Document(request);
doc.toXML();

I am using XOM to build XML documents in Java.

I have created a simple XML document, and I want an XML namespace. But when I set the namespace on the first tag, an empty namespace is set on the childs like xmlns="", how can I get rid of this behaviour? I only want xmlns on the first tag.

I want this XML:

<request xmlns="http://my-namespace">
    <type>Test</type>
    <data>
        <myData>test data</myData>
    </data>
</request>

But this is the XML document output from XOM

<request xmlns="http://my-namespace">
    <type xmlns="">Test</type>
    <data xmlns="">
        <myData>test data</myData>
    </data>
</request>

This is my Java XOM code:

String namespace = "http://my-namespace";
Element request = new Element("request", namespace);

Element type = new Element("type");
type.appendChild("Test");

request.appendChild(type);

Element data = new Element("data");
request.appendChild(data);

Element myData = new Element("myData");
myData.appendChild("test data");
data.appendChild(myData);

Document doc = new Document(request);
doc.toXML();

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(4

十秒萌定你 2024-09-15 22:59:19

这对我有用。但是,我有点困惑为什么 Element 对象不继承其父级的命名空间。 (不是 XML 或 XOM 专家)

代码:

String namespace = "http://my-namespace";
Element request = new Element("request", namespace);

Element type = new Element("type", namespace);
type.appendChild("Test");

request.appendChild(type);

Element data = new Element("data", namespace);
request.appendChild(data);

Element myData = new Element("myData", namespace);
myData.appendChild("test data");
data.appendChild(myData);

Document doc = new Document(request);
System.out.println(doc.toXML());

输出:

<?xml version="1.0"?>
<request xmlns="http://my-namespace">
  <type>Test</type>
  <data>
    <myData>test data</myData>
  </data>
</request>

This works for me. However, I'm a bit puzzled as to why the Element objects don't inherit the namespace of their parents, though. (Not an XML nor XOM expert)

Code:

String namespace = "http://my-namespace";
Element request = new Element("request", namespace);

Element type = new Element("type", namespace);
type.appendChild("Test");

request.appendChild(type);

Element data = new Element("data", namespace);
request.appendChild(data);

Element myData = new Element("myData", namespace);
myData.appendChild("test data");
data.appendChild(myData);

Document doc = new Document(request);
System.out.println(doc.toXML());

Output:

<?xml version="1.0"?>
<request xmlns="http://my-namespace">
  <type>Test</type>
  <data>
    <myData>test data</myData>
  </data>
</request>
请别遗忘我 2024-09-15 22:59:19

我遇到了同样的问题,谷歌引导我来到这里。

@Michael - 这就是javadoc中所说的,是的,但不幸的是,当你实现它时,这不是它的工作原理。除非您执行 Catchwa 的实现,否则子元素将继续获得空白 xmlns 属性。

Catchwa 的实现效果很好。只有我告诉它有命名空间的元素才有命名空间。所有空的 xmlns 属性都消失了。很奇怪。

这是一个错误吗?我似乎无法弄清楚那部分。或者这只是 XOM 的工作方式?

I ran into the same problem, and Google lead me here.

@Michael - That's what it says in the javadoc, yes, but unfortunately, that's not how it works when you implement it. The child elements will continue to get blank xmlns attributes unless you do Catchwa's implementation.

Catchwa's implementation works just fine. Only the element I tell it to have a namespace, has a namespace. All empty xmlns attributes are gone. It's strange.

Is it a bug? I can't seem to figure that part out. Or is it just the way XOM works?

提笔书几行 2024-09-15 22:59:19

不要混淆命名空间和命名空间声明。命名空间是每个元素的固有属性。命名空间声明是“xmlns”属性。尽管它们有联系,但它们不是同一件事。创建元素时,您设置其名称空间,而不是其名称空间声明。

在 XOM 数据模型中,命名空间不是属性。它们是元素本身的固有属性。 XML 中没有任何规则要求元素的子元素与父元素位于同一命名空间中。事实上,理论上文档中的每个元素都可以位于不同的命名空间中。

在 XOM 中,您可以在指定本地名称的同时指定元素或属性的名称空间。当您创建一个元素时,该元素最初没有父元素,因此 XOM 无法默认为该元素提供与其父元素相同的命名空间,即使这是想要的(但事实并非如此)。

当文档被序列化时,命名空间由 xmlnsxmlns:*prefix* 属性表示。 XOM 会确定将这些元素放置在哪里,以匹配您分配给每个元素的命名空间。只需为代码中的每个元素指定所需的命名空间,然后让 XOM 找出放置命名空间声明的位置。

Don't confuse namespaces and namespace declarations. The namespace is an intrinsic property of each element. The namespace declaration is the `xmlns' attribute. They are not the same thing, although they are connected. When you create an element, you set its namespace, not its namespace declaration.

In the XOM data model namespaces are not attributes. They are an intrinsic property of the element itself. There is no rule in XML that requires children of an element to be in the same namespace as the parent. Indeed theoretically every element in the document could be in a different namespace.

In XOM you specify the namespace of an element or attribute at the same time you specify the local name. When you create an element, the element initially has no parent so there's no way for XOM to default to giving the element the same namespace as its parent, even if that's what was wanted (and it's not).

When the document is serialized the namespaces are represented by xmlns and xmlns:*prefix* attributes. XOM figures out where to put these elements to match the namespaces you've assigned to each element. Just specify the namespace you want for each element in your code, and let XOM figure out where to put the namespace declarations.

孤星 2024-09-15 22:59:19

在 XOM 中,您可以 向根元素添加命名空间声明

这是一个包含三个不同名称空间的简短示例:

final String NS_XLINK = "http://www.w3.org/1999/xlink";
final String NS_OTHER = "http://other.com";
Element root = new Element("root", "http://root.com");
root.addNamespaceDeclaration("xlink", NS_XLINK);
root.addNamespaceDeclaration("other", NS_OTHER);
root.addAttribute(new Attribute("xlink:href", NS_XLINK, "http://somewhere.com"));
root.appendChild(new Element("other:alien", NS_OTHER));
Document doc = new Document(root);
System.out.println(doc.toXML());

它产生此结果(为了便于阅读而插入了额外的换行符):

  <?xml version="1.0"?>
  <root
    xmlns="http://root.com"
    xmlns:xlink="http://www.w3.org/1999/xlink"
    xmlns:other="http://other.com"
    xlink:href="http://somewhere.com">
    <other:alien />
  </root>

In XOM you can add a namespace declaration to the root element.

Here's a short example with three different namespaces:

final String NS_XLINK = "http://www.w3.org/1999/xlink";
final String NS_OTHER = "http://other.com";
Element root = new Element("root", "http://root.com");
root.addNamespaceDeclaration("xlink", NS_XLINK);
root.addNamespaceDeclaration("other", NS_OTHER);
root.addAttribute(new Attribute("xlink:href", NS_XLINK, "http://somewhere.com"));
root.appendChild(new Element("other:alien", NS_OTHER));
Document doc = new Document(root);
System.out.println(doc.toXML());

which produces this result (with additional line breaks inserted for readability):

  <?xml version="1.0"?>
  <root
    xmlns="http://root.com"
    xmlns:xlink="http://www.w3.org/1999/xlink"
    xmlns:other="http://other.com"
    xlink:href="http://somewhere.com">
    <other:alien />
  </root>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文