Jump to content

XML DOM wrapper (COM)


eltorro
 Share

Recommended Posts

With autosave on it takes 85 seconds for me, and with it off 87 seconds. Strange. I will have to look deeper into it.

This should get you by in the meantime:

$out = ""
$out &= '<?xml version="1.0"?>' & @CRLF
$out &= '<root>' & @CRLF

For $X = 1 to 10
    $out &= '<child' & $X & '>' & @CRLF
    For $Y = 1 to 10
        $out &= '<grandchild' & $X & '.' & $Y & '>'
        $out &= $X * $Y
        For $Z = 1 to 10
            $out &= '<greatgrandchild' & $X & '.' & $Y & '.' & $Z & '>'
            $out &= $X * $Y * $Z
            $out &= '</greatgrandchild' & $X & '.' & $Y & '.' & $Z & '>' & @CRLF
        Next
        $out &= '</grandchild' & $X & '.' & $Y & '>' & @CRLF
    Next
    $out &= '</child' & $X & '>' & @CRLF
Next

$out &= '</root>' & @CRLF

FileWrite("XML.xml", $out)
Edited by weaponx
Link to comment
Share on other sites

With autosave on it takes 85 seconds for me, and with it off 87 seconds. Strange. I will have to look deeper into it.

This should get you by in the meantime:

$out = ""
$out &= '<?xml version="1.0"?>' & @CRLF
$out &= '<root>' & @CRLF

For $X = 1 to 10
    $out &= '<child' & $X & '>' & @CRLF
    For $Y = 1 to 10
        $out &= '<grandchild' & $X & '.' & $Y & '>'
        $out &= $X * $Y
        For $Z = 1 to 10
            $out &= '<greatgrandchild' & $X & '.' & $Y & '.' & $Z & '>'
            $out &= $X * $Y * $Z
            $out &= '</greatgrandchild' & $X & '.' & $Y & '.' & $Z & '>' & @CRLF
        Next
        $out &= '</grandchild' & $X & '.' & $Y & '>' & @CRLF
    Next
    $out &= '</child' & $X & '>' & @CRLF
Next

$out &= '</root>' & @CRLF

FileWrite("XML.xml", $out)
Thanks for your time and solution, but this is not where I'm looking for.

I want to use the XML DOM wrapper functionality for saving data etc, not the FileWrite or something else.

My excuse for this.

And my test is only to show how slow this function is, maybe someone had a faster script function for me!?!?!

Please, help me!!!

Link to comment
Share on other sites

I need to insert one XML document into another.

I have one file: source.xml that contains:

<?xml version="1.0" encoding="utf-8"?>

<book>

<title lang="en">Everyday Italian</title>

<author>Giada De Laurentiis</author>

<year>2005</year>

<price>30.00</price>

<chapters>

<chapter>Chapter One Title</chapter

<chapter>Chapter Two Title</chapter

<chapter>Chapter Three Title</chapter

</chapters>

</book>

I would like to insert it into library.xml file:

<?xml version="1.0" encoding="utf-8"?>

<library>

<book>

<title lang="en">Italian for Dummies</title>

<author>Larry Smith</author>

<year>2003</year>

<price>19.00</price>

<chapters>

<chapter>Chapter One Title</chapter

<chapter>Chapter Two Title</chapter

<chapter>Chapter Three Title</chapter

</chapters>

</book>

</library>

to get this:

<?xml version="1.0" encoding="utf-8"?>

<library>

<book>

<title lang="en">Italian for Dummies</title>

<author>Larry Smith</author>

<year>2003</year>

<price>19.00</price>

<chapters>

<chapter>Chapter One Title</chapter

<chapter>Chapter Two Title</chapter

<chapter>Chapter Three Title</chapter

</chapters>

</book>

<book>

<title lang="en">Everyday Italian</title>

<author>Giada De Laurentiis</author>

<year>2005</year>

<price>30.00</price>

<chapters>

<chapter>Chapter One Title</chapter

<chapter>Chapter Two Title</chapter

<chapter>Chapter Three Title</chapter

</chapters>

</book>

</library>

I can read and write specific text with the XML DOM wrapper but do not know how to get or move larger chunks of XML.

I have found this UDF and this forum very helpful. Any help witrh this would be appreciated.

Link to comment
Share on other sites

Hello everyone, I'm somewhat new at all of this, and I know this is a simple question, but how do I get values of an xml file? My file is like below:

<?xml version="1.0" encoding="iso-8859-1" ?>
<myxml>
     <category1>
          <option1>A</option1>
          <option2>B</option2>
     </category1>
     <category2>
...
...
...
     </category4>
</myxml>

basically, a standard xml file (this is just an example of the structure). I want to use a function to "get" a value, such as option1, by providing the path. I tried this:

;This should set variable $value to "A"

$value = _xmlgetvalue("/myxml/category1/option1")

Needless to say, it didn't work. Sorry if this is a stupid question, like I said, I'm new to autoit, XML DOM wrapper, and such. Please help me out.

Edited by enkriptix

Free software reviews, suggestions, and computer help: The Free Man's Blog.

Link to comment
Share on other sites

to parse:

<myxml>

<category1>

<option1>A</option1>

<option2>B</option2>

</category1>

<category2>

...

...

...

</category4>

</myxml>

try this (modify file path):

#include <_XMLDomWrapper.au3>

#include <Array.au3>

$File_Path = "C:\Program Files\AutoIt3\XML\Enk.xml"

_XMLFileOpen($File_Path)

$XML_Path = "myxml/category1"

$array = _XMLGetChildren($XML_Path)

_ArrayDisplay ($Array, "test" )

MsgBox (0,'',$Array[1][1])

Link to comment
Share on other sites

to parse:

<myxml>

<category1>

<option1>A</option1>

<option2>B</option2>

</category1>

<category2>

...

...

...

</category4>

</myxml>

try this (modify file path):

#include <_XMLDomWrapper.au3>

#include <Array.au3>

$File_Path = "C:\Program Files\AutoIt3\XML\Enk.xml"

_XMLFileOpen($File_Path)

$XML_Path = "myxml/category1"

$array = _XMLGetChildren($XML_Path)

_ArrayDisplay ($Array, "test" )

MsgBox (0,'',$Array[1][1])

I'm not sure what it is you want us to see. The message box displays "A". I added the line MsgBox (0,'',$Array[1][0]) and it displays "option1".

The child node name in [x>0][0] and it's value in [x>0][1], and possibly a namespace [x>0][2].

Link to comment
Share on other sites

Now I don't want WeaponX to get angry with me but I've read almost all of the pages and haven't seen much to help me out with the "No Xml File." message box.

All I am looking to do is pull 3 values from the following xml file.

<?xml version="1.0"?>
<Batch>
<Case>5555555</Case>
<SubmittedBy>Testerson</SubmittedBy>
<Description>Test</Description>
</Batch>

My script is a "starter" script that doesn't have much to it other than what I copied from a previous post.

#include <array.au3>
#include <_XMLDomWrapper.au3>
_SetDebug(True)
Global $Batch[3]
$sXmlFile = "batchtest.xml"
If @error Then
    MsgBox(4096, "File Open", "No file chosen")
Else
    $oOXml = ""
    $oOXml = _XMLFileOpen($sXmlFile, "")
EndIf
If Not IsObj($oOXml) then
    MsgBox(0,"Error","No Xml file.")
    Exit
EndIf
$xml_path = "Batch"
$Batch[0] = _XMLGetValue($xml_path&"/Case")
$Batch[1] = _XMLGetValue($xml_path&"/SubmittedBy")
$Batch[2] = _XMLGetValue($xml_path&"/Description")


_ArrayDisplay($Batch)

As always thanks for any help and a damn nice UDF.

Link to comment
Share on other sites

#include <Array.au3>
#include <_XMLDomWrapper.au3>

If Not FileExists("batchtest.xml") then
    MsgBox(0,"Error","No Xml file.")
    Exit
EndIf

_XMLFileOpen("batchtest.xml")
$Batch = _XMLGetChildren("Batch");gets child node names and values in array

_ArrayDisplay($Batch);shows array

;to get values, see following
;syntax $Batch[n][1] where n is index number of node (case is 1, submitted by is 2, description is 3)
MsgBox(0,"Test","Case: " & $Batch[1][1] & ", Submitted by: " & $Batch[2][1] & ", Description: " & $Batch[3][1])

Try this...

Free software reviews, suggestions, and computer help: The Free Man's Blog.

Link to comment
Share on other sites

Okay, now I have an issue with a more complex set of values....

I had a namespace problem before, but I solved it as follows:

XML File:

<?xml version="1.0" encoding="iso-8859-1" ?> 
<myxml xmlns="URL">
<category1>
     <sub1>value</sub1>
     <sub2>value</sub2>
</category>
</myxml>

My Code:

$sub2 = _XMLGetData("myxml.xml","category1","sub2")

Func _XMLGetData($file,$node,$child)
    Dim $i, $num, $max, $array, $XML_Path, $value
    _XMLFileOpen($file)
    $XML_Path = "//*[local-name()='" & $node & "']"
    $array = _XMLGetChildren($XML_Path)
    $max = UBound($array)
    For $i = 1 To $max - 1
        If $array[$i][0] = $child Then
            $num = $i
        EndIf
    Next
    $value = $array[$num][1]
    Return $value
EndFunc;==>_XMLGetData

>>this returns the value of sub2. However, next I run into the following problem:

XML File:

<?xml version="1.0" encoding="iso-8859-1" ?> 
<myxml xmlns="URL">
<category1>
     <sub1>value</sub1>
     <sub2 number="1">
         <node1>
               <child1>value</child1>
             <child2>value</child2>
         </node1>
         <node2>
             <child1>value</child1>
             <child2>value</child2>
         </node2>
     </sub2>
     <sub2 number="2">
         <node1>
             <child1>value</child1>
             <child2>value</child2>
         </node1>
         <node2>
             <child1>value</child1>
             <child2>value</child2>
         </node2>
     </sub2>
</category1>
</myxml>

>>>I want to find the value of child2 of node2 of sub2 [number="2"]

Any suggestions?

Edited by enkriptix

Free software reviews, suggestions, and computer help: The Free Man's Blog.

Link to comment
Share on other sites

any chance on parsing the google weather data?

line 3 seems to be defective? there is an extra

mobile_row="0" mobile_zipped="1">

#include <inet.au3>
#include <array.au3>
#include <_XMLDomWrapper.au3>

    InetGet("http://www.google.com/ig/api?weather=Berlin&hl=de", @ScriptDir & "\weather.xml", 1, 0)
    $FileOpen = _XMLFileOpen(@ScriptDir & "\weather.xml")

$curr_data = _GetFirstValue("//xml_api_reply/weather/current_conditions");
ConsoleWrite("current data " & $curr_data & @CRLF);

exit ; 

; retruns array---
Func _GetFirstValue($node)
    $ret_val = _XMLGetValue($node)
    If IsArray($ret_val) Then
        
        _ArrayDisplay($ret_val, '_XMLGetValue  ' & $node)
        
        Return ($ret_val[1])
    Else
        Return SetError(1, 3, 0)
    EndIf
EndFunc   ;==>_GetFirstValue

CODE
<?xml version="1.0" ?>

- <xml_api_reply version="1">

- <weather module_id="0" tab_id="0" mobile_row="0" mobile_zipped="1" row="0" section="0">

mobile_row="0" mobile_zipped="1">

- <forecast_information>

<city data="Berlin, Berlin" />

<postal_code data="Berlin" />

<latitude_e6 data="" />

<longitude_e6 data="" />

<forecast_date data="2009-04-07" />

<current_date_time data="2009-04-07 12:20:00 +0000" />

<unit_system data="SI" />

</forecast_information>

- <current_conditions>

<condition data="Klar" />

<temp_f data="70" />

<temp_c data="21" />

<humidity data="Feuchtigkeit: 43%" />

<icon data="/images/weather/sunny.gif" />

<wind_condition data="Wind: SO mit 14 km/h" />

</current_conditions>

- <forecast_conditions>

<day_of_week data="Di." />

<low data="12" />

<high data="22" />

<icon data="/images/weather/chance_of_rain.gif" />

<condition data="Vereinzelt Regen" />

</forecast_conditions>

- <forecast_conditions>

<day_of_week data="Mi." />

<low data="9" />

<high data="21" />

<icon data="/images/weather/chance_of_storm.gif" />

<condition data="Vereinzelt stürmisch" />

</forecast_conditions>

- <forecast_conditions>

<day_of_week data="Do." />

<low data="7" />

<high data="16" />

<icon data="/images/weather/mostly_sunny.gif" />

<condition data="Meist sonnig" />

</forecast_conditions>

- <forecast_conditions>

<day_of_week data="Fr." />

<low data="10" />

<high data="20" />

<icon data="/images/weather/mostly_sunny.gif" />

<condition data="Teils sonnig" />

</forecast_conditions>

</weather>

</xml_api_reply>

Edited by nobbe
Link to comment
Share on other sites

any chance on parsing the google weather data?

line 3 seems to be defective? there is an extra

mobile_row="0" mobile_zipped="1">

It looks like you copied the code directly from the browser which contains extra formatting. Save to a file, then copy and paste.

Also you are using _XMLGetValue when there are no values to get, this file is all attributes, use _XMLGetAttrib.

Link to comment
Share on other sites

It looks like you copied the code directly from the browser which contains extra formatting. Save to a file, then copy and paste.

Also you are using _XMLGetValue when there are no values to get, this file is all attributes, use _XMLGetAttrib.

thanks , i have i working now (see my code in examples)

the bug was that the "german version" http://www.google.com/ig/api?weather=Berlin&hl=de gives incorrect data and a corrupt XML file, but the english version http://www.google.com/ig/api?weather=Berlin works??

strange..?

Link to comment
Share on other sites

Hallo,

I've got a big problem.

I have to read out an XML File, which I must import to AutoIT.

<Test>

<Data>12345</Data>

<Text>Test1</Text>

<Text1>111</Text1>

<Text2>Yes</Text2>

- <Player>

<Player1>Testplayer</Player1>

<Player2>Testplayer2</Player2>

How can I read out from this XML file the content from Text2 an Player2?

And I should show in on am MsgBox.

I hope that someone can help me.

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

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...