使用 XOM 编写 GraphML?
我正在尝试用 java 中的 XOM 编写 graphML 文档,但我不知道如何使所有名称空间声明正确。为了拥有有效的 graphML,我需要有一个如下所示的根元素:
<graphml xmlns="http://graphml.graphdrawing.org/xmlns"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://graphml.graphdrawing.org/xmlns
http://graphml.graphdrawing.org/xmlns/1.0/graphml.xsd">
我已经能够通过执行以下操作来获得大部分内容
Element root = new Element("graphml");
root.setNamespaceURI("http://graphml.graphdrawing.org/xmlns");
root.addNamespaceDeclaration("xsi", "http://www.w3.org/2001/XMLSchema-instance");
问题是该标签的最后一个元素,xsi:schemaLocation
。我不知道如何在 XOM 中表达这一点。我不能将其作为普通属性来执行,因为这会引发异常(必须声明属性前缀。
),并且将其作为附加命名空间声明也会导致异常(NCNames 不能包含冒号
)。有什么想法吗?
I'm trying to write out a graphML document with XOM in java, but I can't figure out how to get all of the namespace declarations correct. To have valid graphML, I need to have a root element that looks like the following:
<graphml xmlns="http://graphml.graphdrawing.org/xmlns"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://graphml.graphdrawing.org/xmlns
http://graphml.graphdrawing.org/xmlns/1.0/graphml.xsd">
I've been able to get most of this by doing
Element root = new Element("graphml");
root.setNamespaceURI("http://graphml.graphdrawing.org/xmlns");
root.addNamespaceDeclaration("xsi", "http://www.w3.org/2001/XMLSchema-instance");
The problem is the last element of this tag, the xsi:schemaLocation
. I can't figure out how to express this in XOM. I can't do it as a normal attribute, as that throws an exception(Attribute prefixes must be declared.
) and doing it as an additional namespace declaration also results in an exception(NCNames cannot contain colons
). Any ideas?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这应该可以做到。基本上,您没有为
xsi:schemaLocation
属性提供命名空间 URI。因此,尝试创建一个没有命名空间的前缀属性显然是行不通的。在此处检查正确的属性构造函数
属性(字符串名称、字符串 URI、字符串值)
This should do it. Basically you didn't provide the namespace URI for
xsi:schemaLocation
attribute. Thus trying to create a prefixed attribute with no namespace which clearly won't work.Check here for the correct Attribute constructor
Attribute(String name, String URI, String value)