JAVA: Build XML document using XPath expressions

Posted by snoe on Stack Overflow See other posts from Stack Overflow or by snoe
Published on 2008-11-20T21:09:25Z Indexed on 2010/04/15 23:23 UTC
Read the original article Hit count: 268

Filed under:
|
|

I know this isn't really what XPath is for but if I have a HashMap of XPath expressions to values how would I go about building an XML document. I've found dom-4j's DocumentHelper.makeElement(branch, xpath) except it is incapable of creating attributes or indexing. Surely a library exists that can do this?

Map xMap = new HashMap();

xMap.put("root/entity/@att", "fooattrib");
xMap.put("root/array[0]/ele/@att", "barattrib");
xMap.put("root/array[0]/ele", "barelement");
xMap.put("root/array[1]/ele", "zoobelement");

would result in:

<root>
<entity att="fooattrib"/>
<array><ele att="barattrib">barelement</ele></array>
<array><ele>zoobelement</ele></array>
</root>

© Stack Overflow or respective owner

Related posts about Xml

Related posts about xpath