Jump to content
Sign in to follow this  
myspacee

Reading XML nodes (Return with conditional operator)

Recommended Posts

myspacee

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.

Share this post


Link to post
Share on other sites
jdelaney

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.

Share this post


Link to post
Share on other sites
dynamitemedia

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?

Share this post


Link to post
Share on other sites
jdelaney

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)
  • Like 1

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.

Share this post


Link to post
Share on other sites
dynamitemedia

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?

Share this post


Link to post
Share on other sites
jdelaney

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.

Share this post


Link to post
Share on other sites
dynamitemedia

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" 

 

Share this post


Link to post
Share on other sites
jdelaney

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.

Share this post


Link to post
Share on other sites
dynamitemedia

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?

Share this post


Link to post
Share on other sites
jdelaney

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.

Share this post


Link to post
Share on other sites
dynamitemedia

 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

Share this post


Link to post
Share on other sites
jdelaney

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.

Share this post


Link to post
Share on other sites
dynamitemedia

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

Share this post


Link to post
Share on other sites
jdelaney

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
  • Like 1

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.

Share this post


Link to post
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
Sign in to follow this  

  • Similar Content

    • ijourneaux
      By ijourneaux
      I am trying to read an XML file that looks like the following. I am particularly interested in the ParameterNames and ParameterValues
       
      I was able to read a simplier XML file using
      $oXML.load("DataForwardSettings.xml") Local $oInfos = $oXML.selectnodes("//Database") ; or //Info or //Data//Info or //Values/Info  but have not been able to read
      <?xml version="1.0"?> <Entities> <Entity RecordType="TrendData"> <Property Name="AlarmLimitsSetNumber" IsReadOnly="False" ValueType="System.Int32">8</Property> <Property Name="AnalysisParamaterSetNumber" IsReadOnly="False" ValueType="System.Int32">8</Property> <Property Name="ParameterNames" IsReadOnly="True" IsList="True" ListType="List<string>" ValueType="Array" ArrayType="System.String" Count="12">System.Collections.Generic.List`1[System.String]<Data>OVERALL|PK-PK WAVEFORM|HFD|CREST FACTOR|SYNC 1-6|1X|2X|3X-4X|FTF|BSF|BPFO|BPFI</Data></Property> <Property Name="ParameterValues" IsReadOnly="True" IsList="True" ListType="List<float>" ValueType="Array" ArrayType="System.Single" Count="12">System.Collections.Generic.List`1[System.Single]<Data>0.04706,0.27951,0.02640,4.85608,0.03494,0.01727,0.02256,0.01993,0.00207,0.00060,0.00178,0.00221</Data></Property> <Property Name="NumberOfParameters" IsReadOnly="False" ValueType="System.Int32">12</Property> <Property Name="ModifiedSinceLastDataDump" IsReadOnly="False" ValueType="System.Boolean">False</Property> <Property Name="Load" IsReadOnly="False" ValueType="System.Single">0</Property> <Property Name="RPM" IsReadOnly="False" ValueType="System.Single">140.962</Property> <Property Name="Value" IsReadOnly="False" ValueType="System.Single">-1.1E-20</Property> <Property Name="SampleID" IsReadOnly="False" ValueType="System.Int32">-626794</Property> <Property Name="Timestamp_as_String" IsReadOnly="True" ValueType="System.String">8/18/2018 2:05:33 PM</Property> <Property Name="Timestamp_as_UInt" IsReadOnly="False" ValueType="System.UInt32">1534619133</Property> <Property Name="Timestamp" IsReadOnly="False" ValueType="System.DateTime">8/18/2018 2:05:33 PM</Property> <Property Name="StorageFlag" IsReadOnly="False" ValueType="Enum" EnumType="Emerson.CSI.DataImport.MHM.TrendDataStorageType" EnumValue="2">RPM_And_Overall</Property> <Property Name="Parents" IsReadOnly="False" IsList="True" ListType="List<string>" ValueType="Array" ArrayType="System.String" Count="5">System.Collections.Generic.List`1[System.String]<Data>Database=phmhmdb4ts;C:\RBMdbsrv\CustData\4ts_online_1807.rbm;-99|Area=4TS;-494|Equipment=4THTS;-712|MeasurementPoint=D39;-780|DataCollectionSet=Normal Collection Dryer Rolls;-783</Data></Property> </Entity> </Entities> I tried switching to the XML UDF but was alittle lost in how to use it. I am particularly interested in the ParameterNames and ParameterValues.
    • macran
      By macran
      I want to generating a XML file (test.xml) like as follow:
      <?xml version="1.0" encoding="GBK"?>
      <!DOCTYPE SCHEMA SYSTEM "HGWSPZJK.dtd">  ;I can not generate this line
      <SCHEMA CRC="HGWSPZ201808_9131011571786229XM_CRC.XML" SSSQ="201808" CHSNAME="HGWSDKQD" NAME="HGWSPZ">

      <TAXPAYER CJRMC="sigmagroup" CJRDM="9131011571786229XM" CJLX="DKZK" RECORDCOUNT="411" SBRQ="2018-08-31" NSRMC="sigmagroup" SWSBH="9131011571786229XM">

      <Records>
      <Record BZ="" JKKADM="2244" JKKAMC="shanghai" SE="5907.82" TFRQ="2018-08-23" FPHM="224420181000752586-L02"/>
      <Record BZ="" JKKADM="2244" JKKAMC="shanghai" SE="4742.4" TFRQ="2018-08-21" FPHM="224420181000743016-L01"/>
      <Record BZ="" JKKADM="2244" JKKAMC="shanghai" SE="18720" TFRQ="2018-08-14" FPHM="224420181000719215-L01"/>
      </Records>
      </TAXPAYER>
      </SCHEMA>
      I use XML.UDF  
      Local $oXMLile=_XML_CreateFile(@ScriptDir&"\test.xml","",True) 
      but there is no function CreateDocumentType 
      It is no effort even I test use 
      Local $doct=$oXMLfile.CreateDocumentType("SCHEMA", null, "HGWSPZJK.dtd", null)
            $oXmlfile.appendChild($doct)
      pls help me thanks.
       
       
       
       
       
       
    • Skeletor
      By Skeletor
      Hi All,
      This is purely an XML Language question. I need to understand how I can add a value/element in between another XML element. 
      Code below shows the XML file. The info tag has the elements already inserted.
      <Configuration xmlns="http://schemas.datacontract.org/2004/07/Modules.Reporting.DataContracts.LineItems" xmlns:i="http://www.w3.org/2001/XMLSchema-instance"> <GenConf> <Info>ID, Site, Name , Site_ID</Info> </GenConf> </Configuration> Now, I want to add a value from a node group into this code. Something like below. But the example below does not work.
      Any suggestions?
       
      <Configuration xmlns="http://schemas.datacontract.org/2004/07/Modules.Reporting.DataContracts.LineItems" xmlns:i="http://www.w3.org/2001/XMLSchema-instance"> <GenConf> <Info>ID, Site, a/@id, Name , Site_ID</Info> </GenConf> </Configuration> <ProductName> <a id="Windows Server"/> </ProductName>  
    • bhns
      By bhns
      try it for make flyers old games xml + Gdi, i belive many sources had lost 
      AIT-ADRLIST.au3

    • CosminV
      By CosminV
      Hello guys,
       
      This XML things are driving me crazy...
      Please let me know how can I use the _XMLGetAttrib function... I've tried every possible way that I've read on the forum and I cannot succeed reading an attribute.
       
      The XML looks like bellow:
       
      <?xml version="1.0" encoding="UTF-8"?>
      <BATCH attr1= "aaa" attr2="bbb" attr3="ccc">
           <Node1 attr1="aaa" attr2="bbb" attr3="ccc" attr4="ddd" attr5="" attr6="eee" attr7="fff" attr8="ggg"/>
           <Node2 attr1="aaa" attr2="" attr3="" attr4="ddd"/>
           <Node3 attr1="" attr2="" attr3="ccc" attr4="" attr5="" attr6="eee"/>
           <Panel attr1="aaa" STATUS="BBB" attr3="ccc" attr4="ddd" attr5="" attr6="eee">
                <Node5 attr1="aaa" attr2="bbb" attr3="ccc" attr4="ddd" attr5="" attr6="eee" attr7="fff" attr8="ggg"/>
                .............................................................................................................................................................................
           </Panel>
      </Batch>
       
      I need to read the value of STATUS form the Panel node.
       
      Could you please help me?
       
      Best regards,
      Cococnb
×