0

I would like to understand how to create or modify a node in a simple XML file.

the following is the XML file:

<?xml version = "1.0"?>
    <myRootNode>
        <myNode a="P" b="Q" c="R">
            <subNode id= "1">
                <x>more text</x>
                <y>some more</y>
            </subNode>
            <subNode id="2">
                <x>blah</x>
                <y><blah blah</y>
            </subNode>
        </myNode>
        <myNode a="S" b="T" c="U">
            <subNode id= "1">
                <x>some more text</x>
                <y>even more</y>
            </subNode>
            <subNode id="2">
                <x>abcdefg</x>
                <y>hijklmno</y>
            </subNode>
        </myNode>
    </myRootNode>

I would like to know how to edit a node's inner text. for eg: Edit the inner text of the node achieved by evaluating the expression:

/myRootNode/myNode[@a='P' and @b='Q' and @c='R']/subNode[@id='1']/x

I would also like to know how can I use pure xPath to insert a node:

<subNode id="3">
    <x>pqrst</x>
    <y>uvxyz</y>
</subNode>

after the node achieved by evaluating the expression:

/myRootNode/myNode[@a='P' and @b='Q' and @c='R']/subNode[@id="2"]

If possible, please provide Java code. Thank you.

akshitBhatia
  • 1,131
  • 5
  • 12
  • 20
  • XPath is for retrieval only. If you want to modify an XML document, you can either use XSLT or XQuery Update. – dirkk May 06 '14 at 08:59
  • @IanRoberts Can i please have an update on the solution? – akshitBhatia May 06 '14 at 09:10
  • @dirkk How do i use XQuery to make the update/insertion. Can you please provide a solution? – akshitBhatia May 06 '14 at 09:11
  • You have to use **XQuery Update**. However, you will need a processor which supports XQuery Update. You should check first what technology you want to use and then come back with a **specific** question. – dirkk May 06 '14 at 09:40
  • @dirkk I will be making this operation through a Java code. In Java, i am using the spring framework. Can you provide me with a solution for this particular example using xQuery in JAVA Technology along with the packages/apis i need to import to get it working. Please this is urgent. – akshitBhatia May 07 '14 at 11:54
  • Saying it is urgent will **certainly** not lead to me giving faster advice, considering I am doing this freely and unpaid! It is irrelevant if you use Spring or not. It seems like you don't even know what an XQuery processor is and you are unwilling or unable to google stuff like that (or XQuery Update, for that matter) and I am unwilling to help someone who wants to be spoon-feed. – dirkk May 07 '14 at 12:18

2 Answers2

0

Unfortunately, XPath by itself cannot be used to modify XML documents. If possible, try to use http://xmlstar.sourceforge.net/docs.php to modify your xml.

<subNode id="3">
    <x>pqrst</x>
    <y>uvxyz</y>
</subNode>

How can you insert this new node:

xml ed -i "/myRootNode/myNode[@a='P' and @b='Q' and @c='R']/subNode" -t elem -n "subNode" example.xml
xml ed -i "/myRootNode/myNode[@a='P' and @b='Q' and @c='R']/subNode[1]" -t attr -n "id=3" example.xml
xml ed -i "/myRootNode/myNode[@a='P' and @b='Q' and @c='R']/subNode[@id='3']" -t elem -n "x" example.xml
xml ed -i "/myRootNode/myNode[@a='P' and @b='Q' and @c='R']/subNode[@id='3']/x" -t text -n "pqrst" example.xml
xml ed -i "/myRootNode/myNode[@a='P' and @b='Q' and @c='R']/subNode[@id='3']" -t elem -n "y" example.xml
xml ed -i "/myRootNode/myNode[@a='P' and @b='Q' and @c='R']/subNode[@id='3']/y" -t text -n "uvxyz" example.xml

There are a lot of command line options:

  • move
  • delete
  • rename
  • update
  • append
  • insert ...
Gyorgy.Hegedus
  • 361
  • 1
  • 3
0

XQuery is your friend:

insert node <subNode id="3"><x>pqrst</x><y>uvxyz</y></subNode> into //myRootNode
Olc
  • 69
  • 4