Question

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?

Was it helpful?

Solution

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)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top