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 中表达这一点。我不能把它作为一个普通的属性来做,因为它会抛出一个异常(Attribute prefixes must be declared.),而把它作为一个额外的命名空间声明也会导致一个异常(NCNames cannot contain colons)。有任何想法吗?

4

1 回答 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 构造函数

Attribute(String name, String URI, String value)

于 2009-11-23T19:46:23.503 回答