You are not logged in.
Hi All:
I am looking to grab the Host of the primaryserver from the following XML file.
<MappableItem>
<AccountNumber>101000002</AccountNumber>
- <PrimaryServer>
- <Host>
- <![CDATA[ PAUCDP01.local.com ]]>
</Host>
<Port>16384</Port>
</PrimaryServer>
- <SecondaryServer>
- <Host>
- <![CDATA[ PALCDP02.local.com
]]>
</Host>
<Port>16384</Port>
</SecondaryServer>
</MappableItem>
The path would be C:\Program Files\Backup\ConnectionInfo.xml. I am currently using this
if exists file "C:\Program Files\Backup\ConnectionInfo.xml" then line containing "Host" of file "C:\Program Files\Backup\ConnectionInfo.xml" as string else "N/A", but I wind up getting this
<Host>- <![CDATA[ PAUCDP01.local.com ]]> </Host> for my output. Any ideas?
Thanks!
Offline
I personally hate XML parsing and sometimes will be tempted to use the "lines of file" inspectors as you did... but in this straightforward XML, the XML inspectors are pretty simple to use... Use this:
q: node values of child nodes of selects "/MappableItem/PrimaryServer/Host" of xml document of file "C:\Program Files\Backup\ConnectionInfo.xml" as trimmed string
A: PAUCDP01.local.com
Ben
Offline
Thanks Ben, could you please let me know what the if exists would be? I got the dreaded, cannot parse line
If exists (node values of child nodes of selects "/MappableItem/PrimaryServer/Host" of xml document of file "C:\Program Files\Backup\ConnectionInfo.xml") as trimmed string else "NA"
Update: Figured it out myself ![]()
If exsits file "C:\Program Files\Backup\ConnectionInfo.xml" then (node values of child nodes of selects "/MappableItem/PrimaryServer/Host" of xml document of file "C:\Program Files\Backup\ConnectionInfo.xml") as trimmed string else "NA"
Thanks for steering me in the right direction Ben!
Last edited by hbkrules69 (2009-02-11 17:33:59)
Offline
Hi All.
Is there a way to obtain the information of node values of child nodes on a xml file, the problem is that the child nodes have the same name, this is not a normal xml format.
-<xml>
-<Item>
<name>ABCD</name>
<host>hostname</host>
<port>1111</port>
<Item>
-<Item>
<name>EFGH</name>
<host>hostname</host>
<port>2222</port>
<Item>
node value of child nodes of xpaths "/xml/Item/name" of xml document of file "C:\xml.xml"
Error: Singular expression refers to non-unique object.
Thanks
Offline
Thanks Noah.
How can i do to get this result?
(<name>ABCD</name>, <host>hostname</host>), (<name>EFGH</name>, host>hostname</host>)
I'm using:
node values of child nodes of xpaths "/xml/Item/name" of xml document of file "C:\xml.xml"
node values of child nodes of xpaths "/xml/Item/host" of xml document of file "C:\xml.xml"
-<xml>
-<Item>
<name>ABCD</name>
<host>hostname</host>
<port>1111</port>
<Item>
-<Item>
<name>EFGH</name>
<host>hostname</host>
<port>2222</port>
<Item>
Offline
See if this works:
q: (node values of child nodes of xpaths "name" of it, node values of child nodes of xpaths "host" of it) of xpaths "/xml/Item" of xml document of file "C:\temp\test.txt"
A: ABCD, hostname
A: EFGH, hostname
Ben
Offline
Excellent Ben. Thanks for your help
Offline
what if the file is appended and so has duplicate copies of the information specified, is there syntax like "use last line of" that you can append to the above code just to read the latest value ?
Offline
Can you post an example?
Offline
<MappableItem>
<AccountNumber>101000002</AccountNumber>
- <PrimaryServer>
- <Host>
- <![CDATA[ PAUCDP01.local.com ]]>
</Host>
<Port>16384</Port>
</PrimaryServer>
- <SecondaryServer>
- <Host>
- <![CDATA[ PALCDP02.local.com
]]>
</Host>
<Port>16384</Port>
</SecondaryServer>
</MappableItem>
<MappableItem>
<AccountNumber>101000002</AccountNumber>
- <PrimaryServer>
- <Host>
- <![CDATA[ PAUCDP01.local.com ]]>
</Host>
<Port>16384</Port>
</PrimaryServer>
- <SecondaryServer>
- <Host>
- <![CDATA[ PALCDP02.local.com
]]>
</Host>
<Port>16384</Port>
</SecondaryServer>
</MappableItem>
whe I read in the value as above, say the port, what I get returned is 16384,16384 or occurring more depending on how many times the executable si run to create the XMl file.
in my case the clients run this everytime it is rebooted so the file could be appended any amount of times.
Offline
I can only think of one way off the top of my head, but it isn't elegant:
node value of child nodes of selects "/MappableItem/PrimaryServer/Port" of xml document of (("<MappableItem>" & following text of last "<MappableItem>" of preceding text of last "</MappableItem>" of concatenation of lines of file "test.xml") & "</MappableItem>")Here's how this works:
1. Concatenate all lines of the file in to a string
2. Drill down to the text between the last instance of <MappableItem> and the last instance of </MappableItem>
3. Re-append <MappableItem> and </MappableItem> to the beginning and end of the text from step 2.
4. Declare the text as an xml document and perform a select against it.
Honestly, if you just need the last instance of an element, such as <Port>, you may actually be better off using BigFix's string parsing inspectors instead of trying to perform XML selects. I don't really see any advantage to using XML inspectors in your scenario.
Offline
Okay, I have to recant my last statement. I had a moment of clarity at the gym and remembered that there are functions built into XPATH:
node value of child nodes of xpaths "//MappableItem[last()]/PrimaryServer/Port" of xml document of file "test.xml"
Notice the use of last(), which gets the last <MappableItem> node. I also had to use the xpaths inspector instead of selects in order to get it to work.
Offline
nice one, thanks.
Offline