Author |
Message
|
paranoid221 |
Posted: Mon Feb 02, 2009 2:38 am Post subject: Changing the value of a repeating XML Element |
|
|
 Centurion
Joined: 03 Apr 2006 Posts: 101 Location: USA
|
Looking for some guidance on this one. I have the following XML:
Code: |
<Root_Element>
....
...
<Msgs>
<MsgObject>
<MsgObjectChild>
<filePath>/tmp/abc/file1.dat</filePath>
....
</MsgObjectChild>
<MsgObjectChild>
<filePath>/tmp/abc/file2.dat</filePath>
....
</MsgObjectChild>
</MsgObject>
<MsgObject>
<MsgObjectChild>
<filePath>/tmp/abc/file3.dat</filePath>
....
</MsgObjectChild>
<MsgObjectChild>
<filePath>/tmp/abc/file4.dat</filePath>
....
</MsgObjectChild>
</MsgObject>
</Msgs>
</Root_Element> |
After completing other logic inside my java compute node, the last step before I propagate to the out terminal is to update all the file paths so that I'll have /user/tmp/abc/file1.dat ... and so on in place of the original paths in the XML above. so its pretty much appending /user to all the paths.
Traditionally to get the value of an XML element I've used evaluateXPath, getFirstElementByPath and all other supported fun API. But here in this case, it looks like I have to get to one element at a time to update the path. A'ite fine!!! then if thats the case its imperative that I iterate over these XML elements but I do not know how to use the API to get to each occurrence of this XML element. For example if I use evaluateXPath API, what value should I pass as an argument to get to the 'n'th occurence of the filePath.
I'm trying to find the right approach to do this and struggling a bit to even get started to be honest.
Any suggestions appreciated. _________________ LIFE is a series of complex calculations, somewhere multiplied by ZERO.
Last edited by paranoid221 on Mon Feb 02, 2009 10:48 pm; edited 2 times in total |
|
Back to top |
|
 |
mqjeff |
Posted: Mon Feb 02, 2009 5:56 am Post subject: |
|
|
Grand Master
Joined: 25 Jun 2008 Posts: 17447
|
Actually, one of the nice things about XPath is that it tends to return the entire matching node set, and operate on the entire matching node set, by default. This can lead to very poor performing XPath expressions when trying to navigate to the third instance of a deeply nested and widely repeating structure if you're not careful.
So you should be able to write an XPath that says something like "prepend /usr to every element named filePath. |
|
Back to top |
|
 |
paranoid221 |
Posted: Mon Feb 02, 2009 11:02 pm Post subject: |
|
|
 Centurion
Joined: 03 Apr 2006 Posts: 101 Location: USA
|
Appreciate your response Jeffie.
Much to my dismay, it turns out that not only should those values be prepended with another string value but also there is something called as <patchData> element tree elsewhere in the XML which should also go into the value of the <filePath> element.
Which value from the <patchData> element tree to be used is dependent on the index of occurrence of the <filePath> element and thank god the requirement is only a 1 to 1 mapping between these 2 parts of the XML. Or in other words
The value of first occurrence of the element i.e. filePath[0] needs to be changed to /user/tmp/abc/patchid[0]/file1.dat.
The one-to-one correspondence makes it look simple but I still feel left deep in a crater or mebbe the upcoming 2 back-to-back 70+ hr work weeks isn't helping me think any clearer.
I have to see if I can use regular expressions to accomplish this. Uncharted territory for me.
Oh well. fun times already this new year.  _________________ LIFE is a series of complex calculations, somewhere multiplied by ZERO. |
|
Back to top |
|
 |
kimbert |
Posted: Tue Feb 03, 2009 12:48 am Post subject: |
|
|
 Jedi Council
Joined: 29 Jul 2003 Posts: 5542 Location: Southampton
|
Quote: |
I have to see if I can use regular expressions to accomplish this |
Are you considering using regular expressions to modify the incoming XML? If so, I would advise against that.
It should be possible to
Code: |
find the first occurrence of filePath
find the first occurrence of Patch
repeat
modify the value of filePath[n] using Patch[n]
advance to next sibling of filePath
advance to next sibling of Patch
until no more filePath or no more Patch |
|
|
Back to top |
|
 |
paranoid221 |
Posted: Tue Feb 03, 2009 3:07 am Post subject: |
|
|
 Centurion
Joined: 03 Apr 2006 Posts: 101 Location: USA
|
I have the XML stored to an environment variable. I was going to use a regular expression on that copy before I write it to the output.
the way you have advised was the one I first thought of but I'm inside a Java compute node where I do a couple of remote File IO operations after which I need to modify this XML. Iam trying to avoid adding another node just for the purpose of making a change to a few of the fields in the XML.
What I do not know is how to iteratively traverse the XML inside a java compute node using evaluateXPath or getAllElementsByPath etc.
I however do know that if I can get this XML into DOM or SAX, I'd have other ways of doing it. Wondering which would be a better approach.  _________________ LIFE is a series of complex calculations, somewhere multiplied by ZERO. |
|
Back to top |
|
 |
kimbert |
Posted: Tue Feb 03, 2009 3:19 am Post subject: |
|
|
 Jedi Council
Joined: 29 Jul 2003 Posts: 5542 Location: Southampton
|
What's wrong with
Code: |
MbElement filePath = getFirstElementByPath("$Root/XMLNSC/myParent/filePath");
...
filePath = filePath.getNextSibling(); |
|
|
Back to top |
|
 |
|