Tuesday, 25 November 2014

Get and Set XML elements with Namespaces



NEWBIE QUESTION. Hi, haven't worked that much with xml, nothing like this anyway. I have some xml as shown below that I receive which has several Namespaces. The idea is I read some values, then update others before returning the revised xml with Namespaces intact -don't want them removed.


I am given the path to some of the elements like this "cred/sub/aa" or "trip/items/item[0]/customerInfo/custName". But it seems that namespaces make it difficult to get to those elements so simply.


Does anybody know how I can read some of the values like "NON-SMOKING" from custPref or get the value "CABBAGE" from bossman/zz. Also, I want to be able to then set a value such as custName to say "Mr.X".


Any ideas? Thanks.



<?xml version="1.0" encoding="utf-16" ?>
<A1 xmlns:xsi="http://ift.tt/ra1lAU" xmlns:xsd="http://ift.tt/tphNwY">
<cred xmlns="http://ift.tt/1vajMbf">
<sub>
<aa>Zippo</aa>
<bb>lighter</bb>
</sub>
<reqId>
<cc></cc>
<dateOfBirth></dateOfBirth>
</reqId>
</cred>
<reqName xmlns="http://blah-blah/vader/base">qwerty</reqName>
<reqId xmlns="http://blah-blah/vader/base">12345</reqId>
<machine xmlns="http://blah-blah/vader/base">
<qqq>hello</qqq>
<www>goodbye</www>
<eee>99999</eee>
<rrr>88888</rrr>
</machine>
<monkey xmlns="http://blah-blah/vader/base">alskdjfhg</monkey>
<math xmlns="http://blah-blah/vader/base">
<language></language>
</math>
<trip xmlns="http://blah-blah/simple">
<tripOverview xmlns="http://blah-blah/vader/base">
<description></description>
<cost></cost>
</tripOverview>
<bossman xmlns="http://blah-blah/vader/base">
<zz>CABBAGE</zz>
<yy>BANANA</yy>
<xx>MELON</xx>
<ww>SYRUP</ww>
</bossman>
<items>
<item>
<itemSummary xmlns="http://blah-blah/vader/base">
<description></description>
<cost></cost>
<reference></reference>
</itemSummary>
<customerInfo xmlns="http://blah-blah/vader/base">
<custName></custName>
<custPref>NON-SMOKING</custPref>
</customerInfo>
<seatId xmlns="http://blah-blah/vader/base">1</seatId>
</item>
</items>
</trip>
</A1>

No comments:

Post a Comment