eltorro

XML DOM wrapper (COM)

778 posts in this topic

Hi Steve,

Sorry for troubling u..but I am not able to create it:

<PortType><P partialPortBTN="1234561111"/></PortType>

Can u please post an example?

I messed with it for a while and there is a limitation to some of my function in that they aren't setup to take name space args.

Also, not knowing much about namespaces, I think that all the name spaces in the doc header need to be unique. I'll have to check on it to be sure.

see here and here.

The function to create a node with a namespace is obj.createNode(1,"Element","Namespace")

The function to create an attrib node with a namespace is obj.createAttribute("Name","Namespace")

The set the value of the attribute node just created and append it to the parent node.

In the case of the xml in your post all but one of your namespaces are something ="eee". You would use "eee" as your namespace arg. However, since there are several references to "eee" it may cause problems with MSXML. This I will have to check on. Also, I'll have to adjust the functs for namespaces. Namely the createRootNode(wAttrib), CreateChildNode(wAttrib) functions.

Right now I have to get ready to go. I won't be able to get to it until tommorrow sometime.

Steve

p.s.

I had updated a couple of other functions already, however, I am short on time at present.

I am looking at a newer version down the road as I see what other people are needing from the UDF.

Share this post


Link to post
Share on other sites



Please post your xml, in order to provide u an example..

Hmmm....

I'm struggling to figure out a way to step through an xml file and pull values out of a number of sections that look somewhat like this. I'm trying to pull out the tests= values from each section, then my plan is to dump them into a textfile and add them up.

First, I'm just not sure how to step through the xml file and pull out just the "tests" value for each section.

Any help would be awesome!

<testsuite errors="0" failures="0" name="test.com.test.1" tests="5" time="0.274">

<testcase classname="test.com.test.1" name="testGetSessionParmLong" time="0.0050" />

<testcase classname="test.com.test.1" name="testGetSessionParmString" time="0.0" />

<testcase classname="test.com.test.1" name="testChecked" time="0.0" />

<testcase classname="test.com.test.1" name="testParseBoolean" time="0.0" />

<testcase classname="test.com.test.1" name="testGetInitParam" time="0.0" />

<system-out />

<system-err />

</testsuite>

<testsuite errors="0" failures="0" name="test.com.test.2" tests="3" time="0.274">

<testcase classname="test.com.test.2" name="testGetSessionParmLong" time="0.0050" />

<testcase classname="test.com.test.2" name="testGetSessionParmString" time="0.0" />

<testcase classname="test.com.test.2" name="testChecked" time="0.0" />

<system-out />

<system-err />

</testsuite>

...

...

...

Thanks!

Max

Share this post


Link to post
Share on other sites

Hmmm....

I'm struggling to figure out a way to step through an xml file and pull values out of a number of sections that look somewhat like this. I'm trying to pull out the tests= values from each section, then my plan is to dump them into a textfile and add them up.

First, I'm just not sure how to step through the xml file and pull out just the "tests" value for each section.

Any help would be awesome!

<testsuite errors="0" failures="0" name="test.com.test.1" tests="5" time="0.274">

<testcase classname="test.com.test.1" name="testGetSessionParmLong" time="0.0050" />

<testcase classname="test.com.test.1" name="testGetSessionParmString" time="0.0" />

<testcase classname="test.com.test.1" name="testChecked" time="0.0" />

<testcase classname="test.com.test.1" name="testParseBoolean" time="0.0" />

<testcase classname="test.com.test.1" name="testGetInitParam" time="0.0" />

<system-out />

<system-err />

</testsuite>

<testsuite errors="0" failures="0" name="test.com.test.2" tests="3" time="0.274">

<testcase classname="test.com.test.2" name="testGetSessionParmLong" time="0.0050" />

<testcase classname="test.com.test.2" name="testGetSessionParmString" time="0.0" />

<testcase classname="test.com.test.2" name="testChecked" time="0.0" />

<system-out />

<system-err />

</testsuite>

...

...

...

Thanks!

Max

Try the following:
#include "C:\Program Files\AutoIt3\include\_XMLDomWrapper.au3"
_SetDebug (True);show debug messages via console write
;$sXmlFile = "C:\Program Files\AutoIt3\scripts\test.XML"
$sXmlFile = FileOpenDialog("", @ProgramFilesDir & "\autoit\scrpits", "XML (*.xml)", 1)
;ConsoleWrite($sXmlFile&@lf)
If @error Then
    MsgBox(4096, "File Open", "No file chosen")
    Exit
Else
    $oOXml = ""
    $oOXml = _XMLFileOpen ($sXmlFile)
EndIf
Dim $aAttrName[1], $aAttrValue[1], $node
$test = int(InputBox("Test","Enter test# to retrieve:"))
if IsInt($test) Then
   ; get all attribs where tests = ????
    $retval = _XMLGetAllAttrib("//testing/testsuite[@tests="&$test&"]",$aAttrName,$aAttrValue); get all attribs where tests = 5
    if IsArray($aAttrName) Then
        _ArrayDisplay($aAttrName,"Attrib Names")
        _ArrayDisplay($aAttrValue,"Attrib Values")
    Else
        msgbox(0,"Error","Test "&$test&" not found.")
        Exit
    EndIf

    Dim $aAttrName[1], $aAttrValue[1]
   ;get all attribs where tests = ??? and classname attrib name = "testGetSessionParmLong"
    $retval =    _XMLGetAllAttrib('//testing/testsuite[@tests='&$test&']/testcase[@name="testGetSessionParmLong"]',$aAttrName,$aAttrValue)
    if IsArray($aAttrName) Then
    _ArrayDisplay($aAttrName,"Attrib Names")
    _ArrayDisplay($aAttrValue,"Attrib Values")
    Else
        msgbox(0,"Error","Test "&$test&" not found.")
        Exit
    EndIf
EndIf
Exit
#cs
<?xml version="1.0" encoding="UTF-8"?>
<testing>
<testsuite errors="0" failures="0" name="test.com.test.1" tests="5" time="0.274">
<testcase classname="test.com.test.1" name="testGetSessionParmLong" time="0.0050" />
<testcase classname="test.com.test.1" name="testGetSessionParmString" time="0.0" />
<testcase classname="test.com.test.1" name="testChecked" time="0.0" />
<testcase classname="test.com.test.1" name="testParseBoolean" time="0.0" />
<testcase classname="test.com.test.1" name="testGetInitParam" time="0.0" />
<system-out />
<system-err />
</testsuite>
<testsuite errors="0" failures="0" name="test.com.test.2" tests="3" time="0.274">
<testcase classname="test.com.test.2" name="testGetSessionParmLong" time="0.0050" />
<testcase classname="test.com.test.2" name="testGetSessionParmString" time="0.0" />
<testcase classname="test.com.test.2" name="testChecked" time="0.0" />
<system-out />
<system-err />
</testsuite>
</testing>
#ce

Steve.

Share this post


Link to post
Share on other sites

Try the following:

#include "C:\Program Files\AutoIt3\include\_XMLDomWrapper.au3"
_SetDebug (True);show debug messages via console write
;$sXmlFile = "C:\Program Files\AutoIt3\scripts\test.XML"
$sXmlFile = FileOpenDialog("", @ProgramFilesDir & "\autoit\scrpits", "XML (*.xml)", 1)
;ConsoleWrite($sXmlFile&@lf)
If @error Then
    MsgBox(4096, "File Open", "No file chosen")
    Exit
Else
    $oOXml = ""
    $oOXml = _XMLFileOpen ($sXmlFile)
EndIf
Dim $aAttrName[1], $aAttrValue[1], $node
$test = int(InputBox("Test","Enter test# to retrieve:"))
if IsInt($test) Then
  ; get all attribs where tests = ????
    $retval = _XMLGetAllAttrib("//testing/testsuite[@tests="&$test&"]",$aAttrName,$aAttrValue); get all attribs where tests = 5
    if IsArray($aAttrName) Then
        _ArrayDisplay($aAttrName,"Attrib Names")
        _ArrayDisplay($aAttrValue,"Attrib Values")
    Else
        msgbox(0,"Error","Test "&$test&" not found.")
        Exit
    EndIf

    Dim $aAttrName[1], $aAttrValue[1]
  ;get all attribs where tests = ??? and classname attrib name = "testGetSessionParmLong"
    $retval =    _XMLGetAllAttrib('//testing/testsuite[@tests='&$test&']/testcase[@name="testGetSessionParmLong"]',$aAttrName,$aAttrValue)
    if IsArray($aAttrName) Then
    _ArrayDisplay($aAttrName,"Attrib Names")
    _ArrayDisplay($aAttrValue,"Attrib Values")
    Else
        msgbox(0,"Error","Test "&$test&" not found.")
        Exit
    EndIf
EndIf
Exit
#cs
<?xml version="1.0" encoding="UTF-8"?>
<testing>
<testsuite errors="0" failures="0" name="test.com.test.1" tests="5" time="0.274">
<testcase classname="test.com.test.1" name="testGetSessionParmLong" time="0.0050" />
<testcase classname="test.com.test.1" name="testGetSessionParmString" time="0.0" />
<testcase classname="test.com.test.1" name="testChecked" time="0.0" />
<testcase classname="test.com.test.1" name="testParseBoolean" time="0.0" />
<testcase classname="test.com.test.1" name="testGetInitParam" time="0.0" />
<system-out />
<system-err />
</testsuite>
<testsuite errors="0" failures="0" name="test.com.test.2" tests="3" time="0.274">
<testcase classname="test.com.test.2" name="testGetSessionParmLong" time="0.0050" />
<testcase classname="test.com.test.2" name="testGetSessionParmString" time="0.0" />
<testcase classname="test.com.test.2" name="testChecked" time="0.0" />
<system-out />
<system-err />
</testsuite>
</testing>
#ce

Steve.

Right on Steve, thanks for the reply.

Nice UI, but I'm not sure it's pulling out the right data for me.

What I'm trying to do is parse through the entire xml file and hit every <testsuite> section and just pull out the "tests=" value. If you see from the xml I posted, the first section has a "5", meaning 5 tests ran. The second <testsuite> has "3". There might be 20 or 50 more of these and I just want to extract that one value from each section.

any ideas?

thanks!

-max

Share this post


Link to post
Share on other sites

What I'm trying to do is parse through the entire xml file and hit every <testsuite> section and just pull out the "tests=" value. If you see from the xml I posted, the first section has a "5", meaning 5 tests ran. The second <testsuite> has "3". There might be 20 or 50 more of these and I just want to extract that one value from each section.

any ideas?

thanks!

-max

This may work for you

#include "C:\Program Files\AutoIt3\include\_XMLDomWrapper.au3"
_SetDebug (True);show debug messages via console write
;$sXmlFile = "C:\Program Files\AutoIt3\scripts\test.XML"
$sXmlFile = FileOpenDialog("", @ProgramFilesDir & "\autoit\scrpits", "XML (*.xml)", 1)
;ConsoleWrite($sXmlFile&@lf)
If @error Then
    MsgBox(4096, "File Open", "No file chosen")
    Exit
Else
    $oOXml = ""
    $oOXml = _XMLFileOpen ($sXmlFile)
EndIf
Dim $aAttrName[1], $aAttrValue[1], $node
; get all attribs where tests = ????
$retcnt1 = _XMLGetAllAttribNodeCount ('//testing/testsuite', "")
MsgBox(0, "Nodes", "Nodes: " & $retcnt1)
For $j = 0 To $retcnt1 - 1
    $retval = _XMLGetAllAttribIndex ("//testing/testsuite", $aAttrName, $aAttrValue, "", $j); get all attribs where tests = 5
    If IsArray($aAttrName) Then
        _ArrayDisplay($aAttrName, "Attrib Names")
        _ArrayDisplay($aAttrValue, "Attrib Values")
    Else
        MsgBox(0, "Error", "Test: no items not found.")
        Exit
    EndIf
Next
Exit
#cs
    <?xml version="1.0" encoding="UTF-8"?>
    <testing>
    <testsuite errors="0" failures="0" name="test.com.test.1" tests="5" time="0.274">
    <testcase classname="test.com.test.1" name="testGetSessionParmLong" time="0.0050" />
    <testcase classname="test.com.test.1" name="testGetSessionParmString" time="0.0" />
    <testcase classname="test.com.test.1" name="testChecked" time="0.0" />
    <testcase classname="test.com.test.1" name="testParseBoolean" time="0.0" />
    <testcase classname="test.com.test.1" name="testGetInitParam" time="0.0" />
    <system-out />
    <system-err />
    </testsuite>
    <testsuite errors="0" failures="0" name="test.com.test.2" tests="3" time="0.274">
    <testcase classname="test.com.test.2" name="testGetSessionParmLong" time="0.0050" />
    <testcase classname="test.com.test.2" name="testGetSessionParmString" time="0.0" />
    <testcase classname="test.com.test.2" name="testChecked" time="0.0" />
    <system-out />
    <system-err />
    </testsuite>
    </testing>
#ce

But you have to add this code to the _XMLDomWrapper.au3 to make it work

;===============================================================================
; Function Name:    _XMLGetAllAttribNodeCount
; Description:      Get Node Count based on XPath input from root node.
; Parameters:       $path   xml tree path from root node (root/child/child..)
;                   [$query] DOM compliant query string  (not really necessary as it becomes
;part of the path
; Syntax:           _XMLGetAllAttribNodeCount($path,$query)
; Author(s):        Stephen Podhajecki <gehossafats@netmdc.com> & DickB
; Returns:          Number of Nodes found
;                   on error set error to 1 and returns -1
;===============================================================================
Func _XMLGetAllAttribNodeCount($strXPath, $strQry = "")
    Local $objNodeList, $objQueryNodes, $objNode, $arrResponse[2][1], $i, $i1

    $objQueryNodes = $objDoc.documentElement.selectNodes ($strXPath & $strQry)
    If @error = 0 And $objQueryNodes.length > 0 Then
        Return $objQueryNodes.length
    EndIf
;   _XMLError( "Error retrieving attributes for: " & $strXPath & @CRLF & $oMyError.windescription & @CRLF & $oMyError.scriptline)
    _XMLError( "Error retrieving attributes for: " & $strXPath & @CRLF)
    SetError(1)
    Return -1
;   EndIf
EndFunc ;==>_XMLGetAllAttribNodeCount

I hope this works for you

Dick

Share this post


Link to post
Share on other sites

This may work for you

#include "C:\Program Files\AutoIt3\include\_XMLDomWrapper.au3"
_SetDebug (True);show debug messages via console write
;$sXmlFile = "C:\Program Files\AutoIt3\scripts\test.XML"
$sXmlFile = FileOpenDialog("", @ProgramFilesDir & "\autoit\scrpits", "XML (*.xml)", 1)
;ConsoleWrite($sXmlFile&@lf)
If @error Then
    MsgBox(4096, "File Open", "No file chosen")
    Exit
Else
    $oOXml = ""
    $oOXml = _XMLFileOpen ($sXmlFile)
EndIf
Dim $aAttrName[1], $aAttrValue[1], $node
; get all attribs where tests = ????
$retcnt1 = _XMLGetAllAttribNodeCount ('//testing/testsuite', "")
MsgBox(0, "Nodes", "Nodes: " & $retcnt1)
For $j = 0 To $retcnt1 - 1
    $retval = _XMLGetAllAttribIndex ("//testing/testsuite", $aAttrName, $aAttrValue, "", $j); get all attribs where tests = 5
    If IsArray($aAttrName) Then
        _ArrayDisplay($aAttrName, "Attrib Names")
        _ArrayDisplay($aAttrValue, "Attrib Values")
    Else
        MsgBox(0, "Error", "Test: no items not found.")
        Exit
    EndIf
Next
Exit
#cs
    <?xml version="1.0" encoding="UTF-8"?>
    <testing>
    <testsuite errors="0" failures="0" name="test.com.test.1" tests="5" time="0.274">
    <testcase classname="test.com.test.1" name="testGetSessionParmLong" time="0.0050" />
    <testcase classname="test.com.test.1" name="testGetSessionParmString" time="0.0" />
    <testcase classname="test.com.test.1" name="testChecked" time="0.0" />
    <testcase classname="test.com.test.1" name="testParseBoolean" time="0.0" />
    <testcase classname="test.com.test.1" name="testGetInitParam" time="0.0" />
    <system-out />
    <system-err />
    </testsuite>
    <testsuite errors="0" failures="0" name="test.com.test.2" tests="3" time="0.274">
    <testcase classname="test.com.test.2" name="testGetSessionParmLong" time="0.0050" />
    <testcase classname="test.com.test.2" name="testGetSessionParmString" time="0.0" />
    <testcase classname="test.com.test.2" name="testChecked" time="0.0" />
    <system-out />
    <system-err />
    </testsuite>
    </testing>
#ce

But you have to add this code to the _XMLDomWrapper.au3 to make it work

;===============================================================================
; Function Name:    _XMLGetAllAttribNodeCount
; Description:      Get Node Count based on XPath input from root node.
; Parameters:       $path   xml tree path from root node (root/child/child..)
;                   [$query] DOM compliant query string  (not really necessary as it becomes
;part of the path
; Syntax:           _XMLGetAllAttribNodeCount($path,$query)
; Author(s):        Stephen Podhajecki <gehossafats@netmdc.com> & DickB
; Returns:          Number of Nodes found
;                   on error set error to 1 and returns -1
;===============================================================================
Func _XMLGetAllAttribNodeCount($strXPath, $strQry = "")
    Local $objNodeList, $objQueryNodes, $objNode, $arrResponse[2][1], $i, $i1

    $objQueryNodes = $objDoc.documentElement.selectNodes ($strXPath & $strQry)
    If @error = 0 And $objQueryNodes.length > 0 Then
        Return $objQueryNodes.length
    EndIf
;   _XMLError( "Error retrieving attributes for: " & $strXPath & @CRLF & $oMyError.windescription & @CRLF & $oMyError.scriptline)
    _XMLError( "Error retrieving attributes for: " & $strXPath & @CRLF)
    SetError(1)
    Return -1
;   EndIf
EndFunc;==>_XMLGetAllAttribNodeCount

I hope this works for you

Dick

Dick,

Nice work! The popup gave the me the correct number of <testsuite> sections to work with. You included a reference to a function called _XMLGetAllAttribIndex and I'm not seeing that in my _XMLDomWrapper.

-max

Share this post


Link to post
Share on other sites

Dick,

Nice work! The popup gave the me the correct number of <testsuite> sections to work with. You included a reference to a function called _XMLGetAllAttribIndex and I'm not seeing that in my _XMLDomWrapper.

-max

The function is there (at least in the version I have) but it is not mentioned in the header of the UDF. I think it was added later and the header was not updated.

Dick

Share this post


Link to post
Share on other sites

The function is there (at least in the version I have) but it is not mentioned in the header of the UDF. I think it was added later and the header was not updated.

Dick

Dick,

I'm using the Dec 15, 2005 version and I don't see it anywhere within that version of the _XMLDomWrapper.

-max

Share this post


Link to post
Share on other sites

Dick,

I'm using the Dec 15, 2005 version and I don't see it anywhere within that version of the _XMLDomWrapper.

-max

;===============================================================================
; Function Name:    _XMLGetAllAttribIndex
; Description:      Get all XML Field(s) attributes based on Xpathn and specific index.
; Parameters:       $sXpath xml tree path from root node (root/child/child..)
;                   $aNames the array to return the attrib names
;                   $aValue the array to return the attrib values
;                   [$sQuery] DOM compliant query string  (not really necessary as it becomes
;                   [$iNode] node index.
;part of the path
; Syntax:           _XMLGetAllAttribIndex($path,$aNames,$aValues,[$sQuery="",$iNode=0]])
; Author(s):        Stephen Podhajecki <gehossafats@netmdc.com>
; Returns:          array of attrib node names, array of attrib values
;                   on error set error to 1 and returns -1
;===============================================================================
Func _XMLGetAllAttribIndex($strXPath, ByRef $aName, ByRef $aValue, $strQry = "", $NodeIndex = 0)
    Local $objNodeList, $objQueryNodes, $objNode, $arrResponse[2][1], $i, $i1

;   $objQueryNodes = $objDoc.documentElement.selectNodes ($strXPath & $strQry)
    $objQueryNodes = $objDoc.selectNodes ($strXPath & $strQry)
    While @error = 0 And $objQueryNodes.length > 0
        $objNodeList = $objQueryNodes.item ($NodeIndex).attributes
        _DebugWrite("GetAllAttribIndex " & $objNodeList.length)
        ReDim $arrResponse[2][$objNodeList.length + 1]
        ReDim $aName[$objNodeList.length]
        ReDim $aValue[$objNodeList.length]
        For $i = 0 To $objNodeList.length - 1
            $arrResponse[0][$i] = $objNodeList.item ($i).nodeName
            $arrResponse[1][$i] = $objNodeList.item ($i).Value
            $aName[$i] = $objNodeList.item ($i).nodeName
            $aValue[$i] = $objNodeList.item ($i).Value
        Next
        Return $arrResponse
    WEnd
;   _XMLError( "Error retrieving attributes for: " & $strXPath & @CRLF & $oMyError.windescription & @CRLF & $oMyError.scriptline)
    _XMLError( "Error retrieving attributes for: " & $strXPath & @CRLF)
    SetError(1)
    Return -1
;   EndIf
EndFunc ;==>_XMLGetAllAttribIndex

Thanks Dickb

Steve

Share this post


Link to post
Share on other sites

Dick,

I'm using the Dec 15, 2005 version and I don't see it anywhere within that version of the _XMLDomWrapper.

-max

So am I, the UDF header says: Dec 15, 2005

I have no idea where my version came from. But I have downloaded it from the forum, that I am sure about.

I am now searching the forum if I can find it again. If I have I will let you know where it is.

Dick

Share this post


Link to post
Share on other sites

I updated the first post with a link to the latest version. I put the two functions in it provided by Dickb. I removed most of the .documentElement references in the funcs. as it can cause some trouble if there is more than one root element. Also most of the functions that can use namespaces now have an optional parameter to do so. I'll update the func reference in the header shortly.

Any suggestions or additions are welcome and will be evaluated with an open mind.

Steve.

Share this post


Link to post
Share on other sites

;===============================================================================
; Function Name:    _XMLGetAllAttribIndex
; Description:      Get all XML Field(s) attributes based on Xpathn and specific index.
; Parameters:       $sXpath xml tree path from root node (root/child/child..)
;                   $aNames the array to return the attrib names
;                   $aValue the array to return the attrib values
;                   [$sQuery] DOM compliant query string  (not really necessary as it becomes
;                   [$iNode] node index.
;part of the path
; Syntax:           _XMLGetAllAttribIndex($path,$aNames,$aValues,[$sQuery="",$iNode=0]])
; Author(s):        Stephen Podhajecki <gehossafats@netmdc.com>
; Returns:          array of attrib node names, array of attrib values
;                   on error set error to 1 and returns -1
;===============================================================================
Func _XMLGetAllAttribIndex($strXPath, ByRef $aName, ByRef $aValue, $strQry = "", $NodeIndex = 0)
    Local $objNodeList, $objQueryNodes, $objNode, $arrResponse[2][1], $i, $i1

;   $objQueryNodes = $objDoc.documentElement.selectNodes ($strXPath & $strQry)
    $objQueryNodes = $objDoc.selectNodes ($strXPath & $strQry)
    While @error = 0 And $objQueryNodes.length > 0
        $objNodeList = $objQueryNodes.item ($NodeIndex).attributes
        _DebugWrite("GetAllAttribIndex " & $objNodeList.length)
        ReDim $arrResponse[2][$objNodeList.length + 1]
        ReDim $aName[$objNodeList.length]
        ReDim $aValue[$objNodeList.length]
        For $i = 0 To $objNodeList.length - 1
            $arrResponse[0][$i] = $objNodeList.item ($i).nodeName
            $arrResponse[1][$i] = $objNodeList.item ($i).Value
            $aName[$i] = $objNodeList.item ($i).nodeName
            $aValue[$i] = $objNodeList.item ($i).Value
        Next
        Return $arrResponse
    WEnd
;   _XMLError( "Error retrieving attributes for: " & $strXPath & @CRLF & $oMyError.windescription & @CRLF & $oMyError.scriptline)
    _XMLError( "Error retrieving attributes for: " & $strXPath & @CRLF)
    SetError(1)
    Return -1
;   EndIf
EndFunc;==>_XMLGetAllAttribIndex

Thanks Dickb

Steve

You guys are awesome!

I used the code above and used an _ArrayToString to pull out the piece of the array I was interested in. The piece of code allowed me to roll through the entire xml file and grab exactly what I needed. A simple math function on the end added up the column (separated by @CR) and I'm golden.

Thanks much for all the help!

-max

Share this post


Link to post
Share on other sites

Oops, Jumped the gun on the download.

Here are the two function that need to be replaced if you download version 1.0.3.17

;===============================================================================
; Function Name:    _XMLGetNodeCount
; Description:      Get Node Count based on XPath input from root node.
; Parameters:       $path   xml tree path from root node (root/child/child..)
;                   [$query] DOM compliant query string  (not really necessary as it becomes part of the path
;                   $iNodeType  The type of node to count. (element, attrib, comment etc.)
; Syntax:           _XMLGetNodeCount($path,$query)
; Author(s):        Stephen Podhajecki <gehossafats@netmdc.com> & DickB
; Returns:        0 or Number of Nodes found
;                   on error set error to 1 and returns -1
;===============================================================================
Func _XMLGetNodeCount($strXPath, $strQry = "", $iNodeType = 1)
    Local $objNodeList, $objQueryNodes, $objNode, $nodeCount = 0
   ;   $objQueryNodes = $objDoc.documentElement.selectNodes ($strXPath & $strQry)
    $objQueryNodes = $objDoc.selectNodes ($strXPath & $strQry)
    If @error = 0 And $objQueryNodes.length > 0 Then
        For $objNode In $objQueryNodes
            If $objNode.type = $iNodeType Then $nodeCount = $nodeCount + 1
        Next
        Return $nodeCount
    EndIf
   ;    _XMLError( "Error retrieving node count for: " & $strXPath & @CRLF & $oMyError.windescription & @CRLF & $oMyError.scriptline)
    _XMLError( "Error retrieving node count for: " & $strXPath & @CRLF)
    SetError(1)
    Return -1
   ;    EndIf
EndFunc  ;==>_XMLGetNodeCount


;===============================================================================
; Function Name:    -    _DebugWrite($message)
; Description:      -  Writes a message to console with a crlf on the end
; Parameters:       - $message   the message to display
; Syntax:           - _DebugWrite($message)
; Author(s):        -
; Returns:          -
;===============================================================================
Func _DebugWrite($message, $flag =@LF)
    If $debugging Then
        If $flag <> "" Then
            ConsoleWrite($message & $flag )
        Else
            ConsoleWrite($message)
        EndIf
    EndIf
EndFunc  ;==>_DebugWrite

The correct version is now queued if someone wishes to download again.

Steve

Share this post


Link to post
Share on other sites

I'm trying to get the following code imported from a file into an array so that it will store the following in a 2-D array:

state, servicerID, commission, allocation

I'm sorry. I've tried, and I just can't figure it out. Could someone with more intelligence than me please help? :o

Thanks in advance.

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

<allocation>

<state id="AL">

<servicer id="286" commission=".27" allocation=".6" />

<servicer id="1099" commission=".27" allocation=".4" />

</state>

<state id="AZ">

<servicer id="1268" commission=".27" allocation=".6" />

<servicer id="271" comission=".27" allocation=".4" />

</state>

<state id="CA">

<servicer id="202" commission=".27" allocation=".25" />

<servicer id="1077" commission=".27" allocation=".07" />

<servicer id="176" commission=".27" allocation=".08" />

<servicer id="151" commission=".27" allocation=".05" />

<servicer id="128" commission=".27" allocation=".50" />

<servicer id="1704" commission=".27" allocation=".05" />

</state>

<state id="CO">

<servicer id="1109" commission=".27" allocation=".40" />

<servicer id="300" commission=".27" allocation=".60" />

</state>

<state id="CT">

<servicer id="263" commission=".27" allocation=".7" />

<servicer id="100" commission=".27" allocation=".3" />

</state>

<state id="FL">

<servicer id="314" commission=".27" allocation=".5" />

<servicer id="1704" commission=".27" allocation=".25"/>

<servicer id="1232" commission=".27" allocation=".15" />

<servicer id="1610" commission=".27" allocation=".1" />

</state>

<state id="GA">

<servicer id="149" commission=".27" allocation=".7" />

<servicer id="1085" commission=".27" allocation=".3" />

</state>

<state id="IL">

<servicer id="145" commission=".27" allocation=".25" />

<servicer id="1116" commission=".27" allocation=".1" />

<servicer id="144" commission=".27" allocation=".15" />

<servicer id="97" commission=".27" allocation=".5" />

</state>

<state id="IN">

<servicer id="283" commission=".27" allocation=".4" />

<servicer id="97" commission=".27" allocation=".6" />

</state>

<state id="KY">

<servicer id="1266" commission=".27" allocation=".2" />

<servicer id="283" commission=".27" allocation=".3" />

<servicer id="97" commission=".27" allocation=".5" />

</state>

<state id="LA">

<servicer id="1131" commission=".27" allocation=".4" />

<servicer id="261" commission=".27" allocation=".6" />

</state>

<state id="MA">

<servicer id="161" commission=".27" allocation=".7" />

<servicer id="1704" commission=".27" allocation=".3" />

</state>

<state id="MD">

<servicer id="1085" commission=".27" allocation=".6" />

<servicer id="100" commission=".27" allocation=".4" />

</state>

<state id="ME">

<servicer id="161" commission=".27" allocation="1" />

</state>

<state id="MI">

<servicer id="278" commission=".27" allocation=".2" />

<servicer id="1064" commission=".27" allocation=".3" />

<servicer id="97" commission=".27" allocation=".5" />

</state>

<state id="MN">

<servicer id="281" commission=".27" allocation=".5" />

<servicer id="1093" commission=".27" allocation=".2" />

<servicer id="1270" commission=".27" allocation=".3" />

</state>

<state id="MO">

<servicer id="199" commission=".27" allocation="" />

<servicer id="269" commission=".27" allocation="" />

<servicer id="280" commission=".27" allocation="" />

</state>

<state id="MS">

<servicer id="1131" commission=".27" allocation=".4" />

<servicer id="97" commission=".27" allocation=".6" />

</state>

<state id="NC">

<servicer id="123" commission=".27" allocation=".35" />

<servicer id="1085" commission=".27" allocation=".20" />

<servicer id="309" commission=".27" allocation=".45" />

</state>

<state id="NH">

<servicer id="161" commission=".27" allocation="1" />

</state>

<state id="NJ">

<servicer id="256" commission=".27" allocation=".4" />

<servicer id="259" commission=".27" allocation=".6" />

</state>

<state id="NV">

<servicer id="271" commission=".27" allocation=".6" />

<servicer id="310" commission=".27" allocation=".4" />

</state>

<state id="NY">

<servicer id="123" commission=".27" allocation=".14" />

<servicer id="124" commission=".27" allocation=".12" />

<servicer id="299" commission=".27" allocation=".11" />

<servicer id="305" commission=".27" allocation=".13" />

<servicer id="1704" commission=".27" allocation=".5" />

</state>

<state id="OH">

<servicer id="267" commission=".27" allocation=".17" />

<servicer id="127" commission=".27" allocation=".17" />

<servicer id="282" commission=".27" allocation=".16" />

<servicer id="97" commission=".27" allocation=".5" />

</state>

<state id="OR">

<servicer id="202" commission=".27" allocation=".6" />

<servicer id="1074" commission=".27" allocation=".4" />

</state>

<state id="PA">

<servicer id="1065" commission=".27" allocation=".5" />

<servicer id="151" commission=".27" allocation=".3" />

<servicer id="100" commission=".27" allocation=".2" />

</state>

<state id="RI">

<servicer id="161" commission=".27" allocation=".6" />

<servicer id="263" commission=".27" allocation=".4" />

</state>

<state id="SC">

<servicer id="123" commission=".27" allocation=".4" />

<servicer id="294" commission=".27" allocation=".6" />

</state>

<state id="TN">

<servicer id="246" commission=".27" allocation=".33" />

<servicer id="210" commission=".27" allocation=".5" />

<servicer id="1085" commission=".27" allocation=".27" />

<servicer id="283" commission=".27" allocation=".35" />

</state>

<state id="TX">

<servicer id="210" commission=".27" allocation=".1" />

<servicer id="328" commission=".27" allocation=".2" />

<servicer id="100" commission=".27" allocation=".3" />

<servicer id="1704" commission=".27" allocation=".4" />

</state>

<state id="VA">

<servicer id="126" commission=".27" allocation=".4" />

<servicer id="1085" commission=".27" allocation=".6" />

</state>

<state id="VT">

<servicer id="161" commission=".27" allocation="1" />

</state>

</allocation>

Share this post


Link to post
Share on other sites

#35 ·  Posted (edited)

Trying to create/read xml files the files create just fine, but won't read.

I was wondering if it has to do with the string going in is a multiline string, if so not sure how i'm going to deal with that.

Here's the Save and Load functions

Func SaveSnippet($s_Name, $code)
    Local $sFile = $snips_dir & '\' & $s_Name & '.xml'
    While @error = 0
        _XMLCreateFile ($sFile, "Snippet", True)
        _XMLFileOpen ($sFile)
        _XMLCreateRootChild ("Code", $code)
        Return
        ExitLoop
    WEnd
    
    MsgBox(4096, "Error", _XMLError ())
EndFunc ;==>SaveSnippet

Func LoadSnippet($s_Name)
    Local $sFile = $snips_dir & '\' & $s_Name & '.xml', $ret, $s_code
    ConsoleWrite($sFile & @LF)
    If FileExists($sFile) Then
        While @error = 0
            $ret = _XMLFileOpen ($sFile)
            $s_code = _XMLGetValue ("Code")
            Return $s_code
        WEnd
        MsgBox(4096, "Error", _XMLError ())
    EndIf
EndFunc ;==>LoadSnippet

and the error i get on the load

C:\Program Files\AutoIt3\SciTE\Snippets\Snips\Animate Display.xml
Error Retrieving: Code

No matching node(s)found!
Edited by gafrost

SciTE for AutoItDirections for Submitting Standard UDFs

 

Don't argue with an idiot; people watching may not be able to tell the difference.

 

Share this post


Link to post
Share on other sites

Gary,

I haven't played with this UDF yet, but I believe you'll want to use a CDATA section for the snippet.

Dale


Free Internet Tools: DebugBar, AutoIt IE Builder, HTTP UDF, MODIV2, IE Developer Toolbar, IEDocMon, Fiddler, HTML Validator, WGet, curl

MSDN docs: InternetExplorer Object, Document Object, Overviews and Tutorials, DHTML Objects, DHTML Events, WinHttpRequest, XmlHttpRequest, Cross-Frame Scripting, Office object model

Automate input type=file (Related)

Alternative to _IECreateEmbedded? better: _IECreatePseudoEmbedded  Better Better?

IE.au3 issues with Vista - Workarounds

SciTe Debug mode - it's magic: #AutoIt3Wrapper_run_debug_mode=Y Doesn't work needs to be ripped out of the troubleshooting lexicon. It means that what you tried did not produce the results you expected. It begs the questions 1) what did you try?, 2) what did you expect? and 3) what happened instead?

Reproducer: a small (the smallest?) piece of stand-alone code that demonstrates your trouble

Share this post


Link to post
Share on other sites

Gary,

I haven't played with this UDF yet, but I believe you'll want to use a CDATA section for the snippet.

Dale

Yeah I was looking at that also, and tried _XMLCreateCDATA("Code", $code)

But it didn't write the cdata section, matter fact it messed up the file

file after CDATA create

<?xml version="1.0"?>
<Snippet/>

SciTE for AutoItDirections for Submitting Standard UDFs

 

Don't argue with an idiot; people watching may not be able to tell the difference.

 

Share this post


Link to post
Share on other sites

ok, real wierd I can get the example to work, so I figured I would create short test script to make sure nothing else was interfering with the save and load

same problem

test script

#include "Includes\_XMLDomWrapper.au3"

Global $snips_dir = @ScriptDir & "\Snips"
SaveSnippet("Animate_Display", "test")
LoadSnippet("Animate_Display")

Func SaveSnippet($s_Name, $code)
 Local $sFile = $snips_dir & '\' & $s_Name & '.xml', $ret
 While @error = 0
  _XMLCreateFile ($sFile, "Snippet", True)
  _XMLFileOpen ($sFile)
  _XMLCreateRootChild ("Code" , $code)
  Return
  ExitLoop
 WEnd
 
 MsgBox(4096, "Error", _XMLError ())
EndFunc  ;==>SaveSnippet

Func LoadSnippet($s_Name)
 Local $sFile = $snips_dir & '\' & $s_Name & '.xml', $ret, $s_code
 If FileExists($sFile) Then
  While @error = 0
   $ret = _XMLFileOpen ($sFile)
   If @error Then ExitLoop
   $s_code = _XMLGetValue ("Code")
   If @error Then ExitLoop
   Return $s_code
  WEnd
  MsgBox(4096, "Error", _XMLError ())
 EndIf
 Return ""
EndFunc  ;==>LoadSnippet

file

<?xml version="1.0"?>
<Snippet><Code>test</Code></Snippet>

SciTE for AutoItDirections for Submitting Standard UDFs

 

Don't argue with an idiot; people watching may not be able to tell the difference.

 

Share this post


Link to post
Share on other sites

Trying to create/read xml files the files create just fine, but won't read.

I was wondering if it has to do with the string going in is a multiline string, if so not sure how i'm going to deal with that.

Here's the Save and Load functions

Func SaveSnippet($s_Name, $code)
    Local $sFile = $snips_dir & '\' & $s_Name & '.xml'
    While @error = 0
        _XMLCreateFile ($sFile, "Snippet", True)
        _XMLFileOpen ($sFile)
        _XMLCreateRootChild ("Code", $code)
        Return
        ExitLoop
    WEnd
    
    MsgBox(4096, "Error", _XMLError ())
EndFunc;==>SaveSnippet

Func LoadSnippet($s_Name)
    Local $sFile = $snips_dir & '\' & $s_Name & '.xml', $ret, $s_code
    ConsoleWrite($sFile & @LF)
    If FileExists($sFile) Then
        While @error = 0
            $ret = _XMLFileOpen ($sFile)
            $s_code = _XMLGetValue ("Code")
            Return $s_code
        WEnd
        MsgBox(4096, "Error", _XMLError ())
    EndIf
EndFunc;==>LoadSnippet

and the error i get on the load

C:\Program Files\AutoIt3\SciTE\Snippets\Snips\Animate Display.xml
Error Retrieving: Code

No matching node(s)found!
Hi Gary,

This works for me.

global $snips_dir=@ScriptDir
$s_TName ="test.test"
$s_Tcode ="ldkfjlkdjfldjklkafjlakfjeoujocjva ojfoe 9ueruo "&@crlf&"adjkfhajklfhaiof haiueh ioauhlirufhaiouyh oliauh"
SaveSnippet($s_TName,$s_Tcode)
MsgBox(0,"Code",LoadSnippet($s_TName))
Exit

Func SaveSnippet($s_Name, $code)
    Local $sFile = $snips_dir & '\' & $s_Name & '.xml'
    While @error = 0
        _XMLCreateFile ($sFile, "Snippet", True)
        _XMLFileOpen ($sFile)
        _XMLCreateRootChild ("Code", $code)
        Return
        ExitLoop
    WEnd
    
    MsgBox(4096, "Error", _XMLError ())
EndFunc;==>SaveSnippet

Func LoadSnippet($s_Name)
    Local $sFile = $snips_dir & '\' & $s_Name & '.xml', $ret, $s_code
    ConsoleWrite($sFile & @LF)
    If FileExists($sFile) Then
        While @error = 0
            $ret = _XMLFileOpen ($sFile)
            $s_code = _XMLGetValue ("//Code")
            Return $s_code
        WEnd
        MsgBox(4096, "Error", _XMLError ())
    EndIf
EndFunc;==>LoadSnippet

This is the only change I made.

$s_code = _XMLGetValue ("//Code")

I think that It might be better to serialize or hex the string if it is really long as it would eliminate having to escape special characters & ' < >

#Include <String.au3>
global $snips_dir=@ScriptDir
$s_TName ="test.test"
$s_TSource = FileOpen("C:\Program Files\AutoIt3\include\Color.au3",0)
;$s_Tcode ="ldkfjlkdjfldjklkafjlakfjeoujocjva ojfoe 9ueruo "&@crlf&"adjkfhajklfhaiof haiueh ioauhlirufhaiouyh oliauh"
$s_TCode =_StringToHex(FileRead($s_TSource,512))

FileClose($s_TCode)

SaveSnippet($s_TName,$s_Tcode)

MsgBox(0,"Code",_HexToString(LoadSnippet($s_TName)))
Exit

Func SaveSnippet($s_Name, $code)
    Local $sFile = $snips_dir & '\' & $s_Name & '.xml'
    While @error = 0
        _XMLCreateFile ($sFile, "Snippet", True)
        _XMLFileOpen ($sFile)
        _XMLCreateRootChild ("hCode", $code)
        Return
        ExitLoop
    WEnd
    
    MsgBox(4096, "Error", _XMLError ())
EndFunc;==>SaveSnippet

Func LoadSnippet($s_Name)
    Local $sFile = $snips_dir & '\' & $s_Name & '.xml', $ret, $s_code
    ConsoleWrite($sFile & @LF)
    If FileExists($sFile) Then
        While @error = 0
            $ret = _XMLFileOpen ($sFile)
            $s_code = _XMLGetValue ("//hCode")
            Return $s_code
        WEnd
        MsgBox(4096, "Error", _XMLError ())
    EndIf
EndFunc;==>LoadSnippet

Steve

Share this post


Link to post
Share on other sites

I think that It might be better to serialize or hex the string if it is really long as it would eliminate having to escape special characters & ' < >

Steve,

Isn't that one of the main reasons for using CDATA so that you don't need to escape everything (since the CDATA is not parsed)?

Can you comment on the trouble using _XMLCreateCDATA? I reproduced the same outcome that Gary got trying to use it.

thanks,

Dale


Free Internet Tools: DebugBar, AutoIt IE Builder, HTTP UDF, MODIV2, IE Developer Toolbar, IEDocMon, Fiddler, HTML Validator, WGet, curl

MSDN docs: InternetExplorer Object, Document Object, Overviews and Tutorials, DHTML Objects, DHTML Events, WinHttpRequest, XmlHttpRequest, Cross-Frame Scripting, Office object model

Automate input type=file (Related)

Alternative to _IECreateEmbedded? better: _IECreatePseudoEmbedded  Better Better?

IE.au3 issues with Vista - Workarounds

SciTe Debug mode - it's magic: #AutoIt3Wrapper_run_debug_mode=Y Doesn't work needs to be ripped out of the troubleshooting lexicon. It means that what you tried did not produce the results you expected. It begs the questions 1) what did you try?, 2) what did you expect? and 3) what happened instead?

Reproducer: a small (the smallest?) piece of stand-alone code that demonstrates your trouble

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