开发者

Writing GraphML with XOM?

开发者 https://www.devze.com 2022-12-12 12:49 出处:网络
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 lo

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">
开发者_如何学Python

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?


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.

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"));

Check here for the correct Attribute constructor

Attribute(String name, String URI, String value)

0

精彩评论

暂无评论...
验证码 换一张
取 消