I have an xml document like this:
<?xml version="1.0" encoding="UTF-8"?>
<body>
<request>
<location>
<address />
</location>
</request>
</body>
My goal is to insert the value "My Address" into this document in order to have:
<?xml version="1.0" encoding="UTF-8"?>
<body>
<request>
<location>
<address>
My Address
</address>
</location>
</request>
</body>
I have to reach this goal in a java class that must create this document reading it from an XML file (ok) and i have an XPath expression (/request/location/address) that indicates me where to put my text ("My address"). How to implement a Java class that allows me, starting from xml and XPath string, to insert a text into a node? This java class must be universal, not bound to a particular XML structure (this is the reason why I use an XPath expression instead of getting the XMl structure into the class). I hope my question is clear.
/request/location/addresswould not select anything at all in the document as your root element isbodyand notrequest. As for the Java code and XPath, see docs.oracle.com/javase/7/docs/api/javax/xml/xpath/… on how to select a node.