其他分享
首页 > 其他分享> > 用XOM编写GraphML?

用XOM编写GraphML?

作者:互联网

我正在尝试用XOM在Java中写出graphML文档,但是我不知道如何正确地获得所有名称空间声明.为了拥有有效的graphML,我需要有一个如下所示的root元素:

<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不能包含冒号).有任何想法吗?

解决方法:

这应该做.基本上,您没有为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)

标签:graphml,xom,xml,java
来源: https://codeday.me/bug/20191210/2101457.html