BigFix User Group

You are not logged in.

#1 2009-02-11 13:02:55

hbkrules69
Experienced Member
Registered: 2008-09-15
Posts: 55

Read from XML file

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

 

#2 2009-02-11 16:48:37

Ben Kus
Product Expert
From: California
Registered: 2006-07-18
Posts: 4571
Website

Re: Read from XML file

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

 

#3 2009-02-11 17:27:41

hbkrules69
Experienced Member
Registered: 2008-09-15
Posts: 55

Re: Read from XML file

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 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!

Last edited by hbkrules69 (2009-02-11 17:33:59)

Offline

 

#4 2009-06-18 15:35:54

hernan
Member
Registered: 2008-08-25
Posts: 14

Re: Read from XML file

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

 

#5 2009-06-18 16:36:57

noah
Product Manager
From: BigFix
Registered: 2007-05-29
Posts: 466
Website

Re: Read from XML file

Try "node values".

Offline

 

#6 2009-06-19 08:36:27

hernan
Member
Registered: 2008-08-25
Posts: 14

Re: Read from XML file

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

 

#7 2009-06-21 15:03:23

Ben Kus
Product Expert
From: California
Registered: 2006-07-18
Posts: 4571
Website

Re: Read from XML file

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

 

#8 2009-06-22 10:15:02

hernan
Member
Registered: 2008-08-25
Posts: 14

Re: Read from XML file

Excellent Ben.  Thanks for your help

Offline

 

#9 2009-10-01 04:44:00

homer
Active Member
Registered: 2009-06-10
Posts: 24

Re: Read from XML file

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

 

#10 2009-10-01 10:33:36

jessewk
Product Expert
From: Emeryville, CA
Registered: 2006-07-31
Posts: 1352

Re: Read from XML file

Can you post an example?

Offline

 

#11 2009-10-06 06:53:07

homer
Active Member
Registered: 2009-06-10
Posts: 24

Re: Read from 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>
<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

 

#12 2009-10-06 09:36:09

boyd
BES Guru
From: Pennsylvania
Registered: 2009-06-17
Posts: 242
Website

Re: Read from XML file

I can only think of one way off the top of my head, but it isn't elegant:

Code:

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

 

#13 2009-10-06 11:54:11

boyd
BES Guru
From: Pennsylvania
Registered: 2009-06-17
Posts: 242
Website

Re: Read from XML file

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:

Code:

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

 

#14 2009-10-06 23:53:22

homer
Active Member
Registered: 2009-06-10
Posts: 24

Re: Read from XML file

nice one, thanks.

Offline

 

Board footer

Powered by PunBB
© Copyright 2002–2008 PunBB