Read from XML file

(imported topic written by hbkrules6991)

Hi All:

I am looking to grab the Host of the primaryserver from the following XML file.

101000002

16384

  • <![CDATA[ PALCDP02.local.com

]]>

16384

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

- <![CDATA

http:// PAUCDP01.local.com ]

for my output. Any ideas?

Thanks!

(imported comment written by BenKus)

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

(imported comment written by hbkrules6991)

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 :slight_smile:

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!

(imported comment written by hernan91)

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.

-

-

ABCD

hostname

1111

-

EFGH

hostname

2222

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

(imported comment written by NoahSalzman)

Try “node values”.

(imported comment written by hernan91)

Thanks Noah.

How can i do to get this result?

(ABCD, hostname), (EFGH, host>hostname)

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”

-

-

ABCD

hostname

1111

-

EFGH

hostname

2222

(imported comment written by BenKus)

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

(imported comment written by hernan91)

Excellent Ben. Thanks for your help

(imported comment written by homer91)

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 ?

(imported comment written by jessewk)

Can you post an example?

(imported comment written by homer91)

101000002

16384

  • <![CDATA[ PALCDP02.local.com

]]>

16384

101000002

16384

  • <![CDATA[ PALCDP02.local.com

]]>

16384

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.

(imported comment written by MattBoyd)

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 and the last instance of

  3. Re-append and 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 , 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.

(imported comment written by MattBoyd)

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 node. I also had to use the xpaths inspector instead of selects in order to get it to work.

(imported comment written by homer91)

nice one, thanks.

(imported comment written by homer91)

Me again - was wondering is it possible to reference an xml file with varying name as above for parsing, the log being generated now is time stamped

i.e

110621084410.xml

110622092212.xml

and so on

How can i parse data from the latest generated xml file?

(imported comment written by BenKus)

Switch out:

… file "test.xml

with:

files ((maximum of (preceding texts of firsts “.” of names of it as integer ) of files of folder “C:\test”) as string & “.xml”) of folder “C:\test”

Ben

(imported comment written by homer91)

Brillant, thanks very much, worked a charm