gpt4 book ai didi

java - 创建属性为 xsi :type in JDOM2 的 XML 节点

转载 作者:行者123 更新时间:2023-11-30 06:39:06 25 4
gpt4 key购买 nike

我正在尝试创建以下 XML 文档。

<?xml version="1.0" encoding="UTF-8"?>
<BCPFORMAT>
<RECORD>
<FIELD ID="1" xsi:type="CharFixed" MAX_LENGTH="4" />
</RECORD>
</BCPFORMAT>

我使用的 Java 代码如下 -

package com.tutorialspoint.xml;

import java.awt.List;
import java.io.File;
import java.io.FileWriter;
import java.util.ArrayList;

import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.stream.StreamResult;

import org.jdom2.Document;
import org.jdom2.Element;
import org.jdom2.output.Format;
import org.jdom2.output.XMLOutputter;

public class createXmlLayout {
public static void main(String[] args) {
Document doc = new Document();
Element root = new Element("BCPFORMAT");
//RECORD Element
Element child = new Element("RECORD");
//FIELD Element
Element name = new Element("FIELD")
.setAttribute("ID", "1")
.setAttribute("xsi:type", "CharFixed")
.setAttribute("MAX_LENGTH", "4");

child.addContent(name);
root.addContent(child);
doc.addContent(root);

XMLOutputter outputter = new XMLOutputter(Format.getPrettyFormat());
try {
outputter.output(doc, System.out);
outputter.output(doc, new FileWriter("c:\\VTG_MAPN.xml"));

} catch (Exception e) {
e.printStackTrace();
}
}
}

但我收到以下错误:

The name "xsi:type" is not legal for JDOM/XML attributes: XML name 'xsi:type' cannot contain the character ":".

我知道我可能需要使用命名空间,但我无法弄清楚。

最佳答案

由于 : 上的 XML 1.0 规范,JDOM 不允许您以这种方式创建包含冒号 (:) 的路径> 被保留给命名空间。 Check JDOM's FAQ here.

要设置或创建使用命名空间的属性,您必须使用接受命名空间作为参数的函数/构造函数。

在这种情况下,您可以使用以下内容:

e.setAttribute("type", "CharFixed", Namespace.getNamespace("xsi", "xsi_uri"));

更新:

我们可以在子项的父级之一 (FIELD) 中添加命名空间声明,并将子项设置为针对给定属性使用此命名空间。

Namespace namespace = Namespace.getNamespace("xsi", "xsi_uri");
root.addNamespaceDeclaration(namespace);
// ...

Element name = new Element("FIELD")
.setAttribute("ID", "1")
.setAttribute("type", "CharFixed", root.getNamespacesInScope().get(2))
.setAttribute("MAX_LENGTH", "4");

// ...

输出如下:

<?xml version="1.0" encoding="UTF-8"?>
<BCPFORMAT xmlns:xsi="xsi_uri">
<RECORD>
<FIELD ID="1" xsi:type="CharFixed" MAX_LENGTH="4" />
</RECORD>
</BCPFORMAT>

此功能是 NamespaceAware Interface 的一部分

<小时/>

为什么get(2):

如果我们获取根元素的继承命名空间列表,它将返回 3 个命名空间,如以下文本所述:

[Namespace: prefix "" is mapped to URI ""]
[Namespace: prefix "xml" is mapped to URI "http://www.w3.org/XML/1998/namespace"]
[Namespace: prefix "xsi" is mapped to URI "xsi_uri"]

因此,索引 0 是一个空命名空间,索引 1 是默认的 XML 命名空间,最后,索引 2 是为 xsi 添加的命名空间。

<小时/>

当然,我们不想为所需的命名空间硬编码索引,因此,我们可以执行以下操作来预先缓存所需的命名空间:

Namespace xsiNamespace =
root.getNamespacesInScope().stream() // Streams the namespaces in scope
.filter((ns)->ns.getPrefix().equals("xsi")) // Search for a namespace with the xsi prefix
.findFirst() // Stops at the first find
.orElse(namespace); // If nothing was found, returns
// the previously declared 'namespace' object instead.

使用缓存的命名空间:

// ...
.setAttribute("type", "CharFixed", xsiNamespace)
// ...

关于java - 创建属性为 xsi :type in JDOM2 的 XML 节点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44703264/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com