Jump to content

Reading XML nodes (Return with conditional operator)


Recommended Posts

Hello,

try to read simple XML and i decided to make generic script.

I've a value (140403_0_21_00_XX_C044C021_00N_00F) and i MUST retrieve a value in same node id (="21")

<PubPlain>
  <spread0 id="1" pdfname="140403_0_01_54_MN_M048C001_00N_00F" />
  <spread1 id="2" pdfname="140403_0_02_00_XX_C002C047_00N_00F" />
  <spread2 id="3" pdfname="140403_0_03_54_XX_CL16C003_00N_00F" />
  <spread3 id="4" pdfname="140403_0_04_54_XX_C004CL15_00N_00F" />
  <spread4 id="5" pdfname="140403_0_05_54_XX_ML14M005_00N_00F" />
  <spread5 id="6" pdfname="140403_0_06_54_XX_C006CL13_00N_00F" />
  <spread6 id="7" pdfname="140403_0_07_54_XX_CL12C007_00N_00F" />
  <spread7 id="8" pdfname="140403_0_08_54_XX_C008CL11_00N_00F" />
  <spread8 id="9" pdfname="140403_0_09_54_XX_CL10C009_00N_00F" />
  <spread9 id="10" pdfname="140403_0_10_54_XX_M010CL09_00N_00F" />
  <spread10 id="11" pdfname="140403_0_11_54_XX_CL08C011_00N_00F" />
  <spread11 id="12" pdfname="140403_0_12_54_XX_C012CL07_00N_00F" />
  <spread12 id="13" pdfname="140403_0_13_54_XX_CL06C013_00N_00F" />
  <spread13 id="14" pdfname="140403_0_14_54_XX_C014CL05_00N_00F" />
  <spread14 id="15" pdfname="140403_0_15_54_XX_CL04C015_00N_00F" />
  <spread15 id="16" pdfname="140403_0_16_54_XX_C016CL03_00N_00F" />
  <spread16 id="17" pdfname="140403_0_17_54_XX_CL02C017_00N_00F" />
  <spread17 id="18" pdfname="140403_0_18_54_XX_C018CL01_00N_00F" />
  <spread18 id="19" pdfname="140403_0_19_00_XX_C046C019_00N_00F" />
  <spread19 id="20" pdfname="140403_0_20_00_XX_C020C045_00N_00F" />
  <spread20 id="21" pdfname="140403_0_21_00_XX_C044C021_00N_00F" />
  <spread21 id="22" pdfname="140403_0_22_00_XX_M022C043_00N_00F" />
  <spread22 id="23" pdfname="140403_0_23_00_XX_C042C023_00N_00F" />
  <spread23 id="24" pdfname="140403_0_24_00_XX_C024C041_00N_00F" />
  <spread24 id="25" pdfname="140403_0_25_00_XX_M040C025_00N_00F" />
  <spread25 id="26" pdfname="140403_0_26_00_XX_C026C039_00N_00F" />
  <spread26 id="27" pdfname="140403_0_27_00_XX_C038C027_00N_00F" />
  <spread27 id="28" pdfname="140403_0_28_00_XX_C028C037_00N_00F" />
  <spread28 id="29" pdfname="140403_0_29_00_XX_C036C029_00N_00F" />
  <spread29 id="30" pdfname="140403_0_30_00_XX_C030C035_00N_00F" />
  <spread30 id="31" pdfname="140403_0_31_00_XX_M034C031_00N_00F" />
  <spread31 id="32" pdfname="140403_0_32_00_XX_C032C033_00N_00F" />
</PubPlain>

My XMLs are simple but tend to vary in certain conditions. So i haven't any sure foothold, I must read XML everytime.

I've read several approaches to XML reading but can't determine which best.

(dead?) _XMLDomWrapper.au3 seems to be the best choice;

can you suggest best way to implement XML reading in AI ?

Thank you,

m.

Link to comment
Share on other sites

This is with no error handling...your script will blowup if the 'pdfname' is not found...you can break it out, and do the error handling yourself:

$file = @DesktopDir & "\temp.xml"
$oXML = ObjCreate("Microsoft.XMLDOM")
$oXML.load($file)
$id = $oXML.selectSingleNode("//PubPlain/*[@pdfname='140403_0_21_00_XX_C044C021_00N_00F']").GetAttribute("id")
ConsoleWrite($id & @CRLF)
IEbyXPATH-Grab IE DOM objects by XPATH IEscriptRecord-Makings of an IE script recorder ExcelFromXML-Create Excel docs without excel installed GetAllWindowControls-Output all control data on a given window.
Link to comment
Share on other sites

  • 7 months later...

i have been trying to get this to work using a code that you gave me in another thread but its not working at all  i want to search the xml by  ID=2 for example

$file = "<channels><channel ID='2' url='http://videe.com/index.php/channel/3a9a071' name='Guest1588664 live from Android' quality='best'/> <channel ID='3' url='http://videe.com/index.php/channel/ea579cc92d' name='boulon live' quality='best'/> <channel ID='4' url='http://videe.com/index.php/channel/290a3a52b' name='frank11262' quality='best'/> <channel ID='5' url='http://videe.com/index.php/channel/ab6c066e' name='REAL MADRID  #  LIVERPOOL 19:45 gmt' quality='best'/> </channels>"


$oXML = ObjCreate("Microsoft.XMLDOM")
$oXML.load($file)
$id = $oXML.selectSingleNode("//channels/*[@ID='2']").GetAttribute("url")
msgbox(4096, "test", $id)

but i am getting nothing back

what am i doing wrong?

Link to comment
Share on other sites

use .load() when you are loading a file...when loading a string...

$file = "<channels><channel ID='2' url='http://videe.com/index.php/channel/3a9a071' name='Guest1588664 live from Android' quality='best'/> <channel ID='3' url='http://videe.com/index.php/channel/ea579cc92d' name='boulon live' quality='best'/> <channel ID='4' url='http://videe.com/index.php/channel/290a3a52b' name='frank11262' quality='best'/> <channel ID='5' url='http://videe.com/index.php/channel/ab6c066e' name='REAL MADRID  #  LIVERPOOL 19:45 gmt' quality='best'/> </channels>"

$oXML = ObjCreate("Microsoft.XMLDOM")
$oXML.loadxml($file)
$id = $oXML.selectSingleNode("//channels/*[@ID='2']").GetAttribute("url")
msgbox(4096, "test", $id)
IEbyXPATH-Grab IE DOM objects by XPATH IEscriptRecord-Makings of an IE script recorder ExcelFromXML-Create Excel docs without excel installed GetAllWindowControls-Output all control data on a given window.
Link to comment
Share on other sites

well now after further testing  when i try to load a XML file it come back blank and this is the code i used and XML looks the same in the variable above.

$xml = @ScriptDir &  "\videe.xml"
msgbox(4096, "test", $xml)


$oXML = ObjCreate("Microsoft.XMLDOM")
$oXML.load($xml)
$id = $oXML.selectSingleNode("//channels/*[@ID='41']").GetAttribute("url")
; $id = $oXML.selectSingleNode("//*[@ID='41']").GetAttribute("url")

msgbox(4096, "test", $id & @CRLF)

is it cause the variable you used above is like all 1 line of XML?

Link to comment
Share on other sites

If the XML is not valid, then you can't use the xmldom.

Put consolewrite($oXML.xml & @crlf) after you load in the xml...if it's empty, then the XML is not valid.  Otherwise, I can't read minds, or files on outside computers.

Edited by jdelaney
IEbyXPATH-Grab IE DOM objects by XPATH IEscriptRecord-Makings of an IE script recorder ExcelFromXML-Create Excel docs without excel installed GetAllWindowControls-Output all control data on a given window.
Link to comment
Share on other sites

the xml i have is just as we had above and looks like this:

<channels>
<channel ID='2' url='http://videe.com/index.php/channel/3a9a071' name='Guest1588664 live from Android' quality='best'/>
<channel ID='3' url='http://videe.com/index.php/channel/ea579cc92d' name='boulon live' quality='best'/>
<channel ID='4' url='http://videe.com/index.php/channel/290a3a52b' name='frank11262' quality='best'/>
<channel ID='5' url='http://videe.com/index.php/channel/ab6c066e' name='REAL MADRID  #  LIVERPOOL 19:45 gmt' quality='best'/>
</channels>

you had this above

$file = "<channels><channel ID='2' url='http://videe.com/index.php/channel/3a9a071' name='Guest1588664 live from Android' quality='best'/> <channel ID='3' url='http://videe.com/index.php/channel/ea579cc92d' name='boulon live' quality='best'/> <channel ID='4' url='http://videe.com/index.php/channel/290a3a52b' name='frank11262' quality='best'/> <channel ID='5' url='http://videe.com/index.php/channel/ab6c066e' name='REAL MADRID  #  LIVERPOOL 19:45 gmt' quality='best'/> </channels>"

i do not get what is the difference in these files?  but its coming back  

>"C:Program FilesAutoIt3SciTE..autoit3.exe" /ErrorStdOut "C:Documents and SettingssettopDesktopsearch XML.au3" 

 

Link to comment
Share on other sites

That doesn't help...again, can't read minds, or your consol output...you only provided a small portion of a total picture that's needed to provide any constructive help.

IEbyXPATH-Grab IE DOM objects by XPATH IEscriptRecord-Makings of an IE script recorder ExcelFromXML-Create Excel docs without excel installed GetAllWindowControls-Output all control data on a given window.
Link to comment
Share on other sites

sorry but i gave you exactly what i was working with and what the console said which was blank...

i came up with a way to get it to work for now and since the XML may be formatted incorrectly it is the way i get it.

so i did this and it seems to work 

#include <Process.au3>
#include <Constants.au3>
#include <String.au3>
#include <Array.au3>

   Local Const $xmlFile = 'C:\Documents and Settings\settop\Desktop\videe.xml'
   $xml=FileRead($xmlFile)
   $colName ="2"
   $sSearch=_StringBetween($xml,"<channel ID='" & $colName & "'","/>")
   $sResult=_ArrayToString($sSearch,"")

   Local $splitXML = StringSplit($sResult, "'")
   For $i = 1 To $splitXML[0]
   Next

   msgbox(0, "URL",  $splitXML[2] & @CRLF)
   msgbox(0, "Name",  $splitXML[4] & @CRLF)
   msgbox(0, "Quality",  $splitXML[6] & @CRLF)

and once again the "xml" looks like this:

<channels>
<channel ID='2' url='http://videe.com/index.php/channel/3a9a071' name='Guest1588664 live from Android' quality='best'/> 
<channel ID='3' url='http://videe.com/index.php/channel/ea579cc92d' name='boulon live' quality='best'/> 
<channel ID='4' url='http://videe.com/index.php/channel/290a3a52b' name='frank11262' quality='best'/> 
<channel ID='5' url='http://videe.com/index.php/channel/ab6c066e' name='REAL MADRID  #  LIVERPOOL 19:45 gmt' quality='best'/> 
</channels>

it must not be reading it as XML .   do you see what i did and is there an easier way to do it?

Link to comment
Share on other sites

Works fine for me:

ConsoleWrite(FileRead("some.xml") & @CRLF)
$oXML = ObjCreate("Microsoft.XMLDOM")
$oXML.load("some.xml")
ConsoleWrite($oXML.xml & @CRLF)
$id = $oXML.selectSingleNode("//channels/*[@ID='2']").GetAttribute("url")
ConsoleWrite($id & @CRLF)

output:

<channels>
<channel ID='2' url='http://videe.com/index.php/channel/3a9a071' name='Guest1588664 live from Android' quality='best'/>
<channel ID='3' url='http://videe.com/index.php/channel/ea579cc92d' name='boulon live' quality='best'/>
<channel ID='4' url='http://videe.com/index.php/channel/290a3a52b' name='frank11262' quality='best'/>
<channel ID='5' url='http://videe.com/index.php/channel/ab6c066e' name='REAL MADRID  #  LIVERPOOL 19:45 gmt' quality='best'/>
</channels>
<channels>
 <channel ID="2" url="http://videe.com/index.php/channel/3a9a071" name="Guest1588664 live from Android" quality="best"/>
 <channel ID="3" url="http://videe.com/index.php/channel/ea579cc92d" name="boulon live" quality="best"/>
 <channel ID="4" url="http://videe.com/index.php/channel/290a3a52b" name="frank11262" quality="best"/>
 <channel ID="5" url="http://videe.com/index.php/channel/ab6c066e" name="REAL MADRID  #  LIVERPOOL 19:45 gmt" quality="best"/>
</channels>

http://videe.com/index.php/channel/3a9a071

IEbyXPATH-Grab IE DOM objects by XPATH IEscriptRecord-Makings of an IE script recorder ExcelFromXML-Create Excel docs without excel installed GetAllWindowControls-Output all control data on a given window.
Link to comment
Share on other sites

 that is strange,  am i missing an include file or something?

cause it fails printing to console on the second console write command here:

$oXML.load("C:\Documents and Settings\settop\Desktop\videe.xml")
ConsoleWrite($oXML.xml & @CRLF)

or is it just correct xml?

the first one works and prints out just fine

Edited by dynamitemedia
Link to comment
Share on other sites

No includes are necessary...and you are still not posting back enough information...why didn't you include the fileread output?

I want the output from the consol, not what you think the file contains.

Edited by jdelaney
IEbyXPATH-Grab IE DOM objects by XPATH IEscriptRecord-Makings of an IE script recorder ExcelFromXML-Create Excel docs without excel installed GetAllWindowControls-Output all control data on a given window.
Link to comment
Share on other sites

sorry cause honestly totally new to this and not 100% what i am supposed to show... i am only getting the 1st one showing in console and second one shows nothing   

this is the whole code, the fileread output works just fine but its a lot so i commented it out

;ConsoleWrite(FileRead("C:\Documents and Settings\settop\Desktop\videe.xml") & @CRLF)
$oXML = ObjCreate("Microsoft.XMLDOM")
$oXML.load("C:\Documents and Settings\settop\Desktop\Desktop\videe.xml")
ConsoleWrite($oXML.xml & @CRLF)
$id = $oXML.selectSingleNode("//channels/*[@ID='2']").GetAttribute("url")
ConsoleWrite($id & @CRLF)

console read this:

>"C:\Program Files\AutoIt3\SciTE\..\autoit3.exe" /ErrorStdOut "C:\Documents and Settings\settop\Desktop\test.au3"    


>Exit code: 0    Time: 0.3512

If that is not what your asking for i am sorry cause i just started using this 2 days ago i am used to bat and echo etc...  i was using Notepad++ till yesterday as well.   Not trying to be difficult just not familiar with the program yet 

Edited by dynamitemedia
Link to comment
Share on other sites

Well, obviously, your xml does not contain exactly what you posted above...and since I can't see it, I assume it's faulty.

Good luck.

Keep going the regular expression route...I'm not the best at those, so would leave it to others to answer.

Edited by jdelaney
IEbyXPATH-Grab IE DOM objects by XPATH IEscriptRecord-Makings of an IE script recorder ExcelFromXML-Create Excel docs without excel installed GetAllWindowControls-Output all control data on a given window.
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...