使用XOM编写GraphML?

3

我正在尝试使用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 不能包含冒号)。有什么建议吗?
1个回答

3

这应该就可以了。基本上,您没有为xsi:schemaLocation属性提供命名空间URI。因此,尝试创建一个没有命名空间的带前缀的属性显然是行不通的。

root.addAttribute(new Attribute("xsi:schemaLocation",
    "http://www.w3.org/2001/XMLSchema-instance",
    "http://graphml.graphdrawing.org/xmlns http://graphml.graphdrawing.org/xmlns/1.0/graphml.xsd"));

在这里检查正确的属性构造函数

Attribute(String name, String URI, String value)

该链接提供了正确的属性构造函数,包括名称、URI和值。请确保使用此构造函数以正确设置属性。

谢谢,这个对于实现Sitemaps.org验证很有效。http://www.sitemaps.org/protocol.html - ronnyfm

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接