Sign in to follow this  
Followers 0
toofat

XML help

12 posts in this topic

This is my XML:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<ns2:SomeData serviceName="leaguetable">
    </Sport name="Soccer">
    </Sport name="Tennis">
</ns2:SomeData>

I'm trying to get the atribute value of both nodes.

What am I doing wrong?

$oXML = ObjCreate('MSXML2.DOMDocument')
 If NOT IsObj($oXML) Then Exit
 $oXML.async = false
 $oXML.setProperty('SelectionLanguage', 'XPath')
 $oXML.load($xmlPath)
 If $oXML.parseError.errorCode <> 0 Then Exit

 For $oSport in $oXML.selectNodes('/ns2:SomeData/Sport')
  $myvar = $oSport.getAttribute('name')
  MsgBox(0,'', $myvar)
 Next

Exit

Also, how to get the value of this "serviceName" attribute in the root node?

I'm getting that my problem is this node name "ns2:SomeData", but how do you work with this? Please help.

Share this post


Link to post
Share on other sites



Not valid XML.  Also, unable to get it to work with the namespaces:

$string = '<?xml version="1.0"?>' & @CRLF & _
'<SomeData serviceName="leaguetable">' & @CRLF & _
    '<Sport name="Soccer" />' & @CRLF & _
    '<Sport name="Tennis" />' & @CRLF & _
'</SomeData>'

 $oXML = ObjCreate("Microsoft.XMLDOM")
 If NOT IsObj($oXML) Then Exit
 $oXML.loadxml($string)

 For $oSport in $oXML.selectNodes('//Sport')
    $myvar = $oSport.getAttribute('name')
    MsgBox(0,'', $myvar)
 Next

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

"jdelaney" yes, sorry, this would be valid XML:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<ns2:SomeData serviceName="leaguetable">
    <Sport name="Soccer" />
    <Sport name="Tennis" />
</ns2:SomeData>

But you didn't answered my question: How to work with node name "ns2:SomeData".

I have found a work-around (in the last 20 minutes) like this:

$oXML = ObjCreate('MSXML2.DOMDocument')
 If NOT IsObj($oXML) Then Exit
 $oXML.async = false
 $oXML.setProperty('SelectionLanguage', 'XPath')
 $oXML.load($xmlPath)
 If $oXML.parseError.errorCode <> 0 Then Exit

 For $oSport in $oXML.selectNodes('/*/Sport')
  $myvar = $oSport.getAttribute('name')
  MsgBox(0,'', $myvar)
 Next

Exit

but still, I would like to know how to work with these kind of node names.

When you sad the XML is not valid, did you also mean that a node name "ns2:SomeData" is not valid?

If so, why??? I'm curious because I will be getting a whole bunch of those in a couple of weeks from now :-))

Share this post


Link to post
Share on other sites

Best I can do for you.  Not too familiar with namespaces...I think you need to link to an XSD for their definitions, or something...again, not sure:

$string = '<?xml version="1.0"?>' & @CRLF & _
'<ns2:SomeData serviceName="leaguetable">' & @CRLF & _
    '<Sport name="Soccer" />' & @CRLF & _
    '<Sport name="Tennis" />' & @CRLF & _
'</ns2:SomeData>'
$string = StringRegExpReplace($string,"(</?)(\w+:)","$1")
ConsoleWrite($string & @CRLF)

 $oXML = ObjCreate('MSXML2.DOMDocument')
 $oXML.loadxml($string)
 $oXML.setProperty('SelectionLanguage', 'XPath')


 For $oSport in $oXML.selectNodes('//SomeData/Sport')
    $myvar = $oSport.getAttribute('name')
    MsgBox(0,'', $myvar)
 Next

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

Whou, you have gone the other way. Isn't regular expressions just to slow for this? And what about reading atributes?

Sorry but MSXML.DOM is my cup of tea for this because this XML is 1,5MB in it's original size.

OK, I think I get it, I will try to learn more about "namespaces" and XSD. For now thou, selecting the node like "/*" will have to do.

Thank you.

Share this post


Link to post
Share on other sites

Like I said, you need to look into namespaces.

I've created a workaround to remove them.


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

I'm a freaking idiot and you are genious. Great work man. I should pay more attention. I still think my work-around is better but nice work never the less. Thank you :thumbsup: .

Share this post


Link to post
Share on other sites

#8 ·  Posted (edited)

Got a little closer, at least it's recognized as xml, now:

$string = '<?xml version="1.0"?>' & @CRLF & _
'<ns2:SomeData xmlns:ns2="http://www.w3.org/TR/html4/" serviceName="leaguetable">' & @CRLF & _
    '<Sport name="Soccer" />' & @CRLF & _
    '<Sport name="Tennis" />' & @CRLF & _
'</ns2:SomeData>'
;~ $string = StringRegExpReplace($string,"(</?)(\w+:)","$1")
;~ ConsoleWrite($string & @CRLF)

parse($string)
Func parse($xml)
    
    $oXML = ObjCreate('MSXML2.DOMDocument')
    $oXML.loadxml($xml)
    $oXML.setProperty('SelectionLanguage', 'XPath')

    ConsoleWrite($oXML.xml & @CRLF)


    For $oSport in $oXML.selectNodes('//ns2:SomeData/Sport')
        $myvar = $oSport.getAttribute('name')
        MsgBox(0,'', $myvar & " " & $oSport.parentnode.nodename)
    Next
EndFunc

So weird, but can validate against the parent node name...only msgbox those with the parent you expect it to have:

Func parse($xml)
    $oXML = ObjCreate("Microsoft.XMLDOM")
    $oXML.loadxml($xml)
    ConsoleWrite($oXML.xml & @CRLF)

    For $oSport in $oXML.selectNodes("//Sport")
        If String($oSport.parentnode.nodename) = "ns2:SomeData" Then
            $myvar = $oSport.getAttribute('name')
            MsgBox(0,'', $myvar & " " & $oSport.parentnode.nodename)
        EndIf
    Next
EndFunc
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

You seem to have gotten a working solution, but here is how I work with XML files. In my situation, I have to d/l at least 20 or so XML files of only about 5kb each, and the approach I take may not work for you or help you, but if it does, Great! :) . I have pasted shortened versions of the code I use and one of the XML files I work with.

 

#include <String.au3>
#include <File.au3>
#include <Array.au3>

Local $Products, $Product_Array, $Rebate_Array, $Title, $CWR_SKU

_FileReadToArray("example.xml", $Product_Array)

$Products     = _StringBetween($Product_Array[UBound($Product_Array) - 2], 'product_count total="', '"/>')
$Rebate_Array = _ArrayFindAll($Product_Array, 'rebate status="Y"', 0, 0, 0, 1)

If @error Then
    ConsoleWrite("No rebates." & @CRLF)
Else
    ConsoleWrite(UBound($Rebate_Array) & " rebates." & @CRLF)
    For $a = 0 To UBound($Rebate_Array) - 1
        $CWR_SKU        = _StringBetween($Product_Array[$Rebate_Array[$a] - 3], 'cwr_sku="', '">')
        $Title          = _StringBetween($Product_Array[$Rebate_Array[$a] - 2], 'description title="', '"/>')
        ConsoleWrite($CWR_SKU[0] & ", " & $Title[0] & @CRLF)
    Next
EndIf
 

example.xml

<?xml version="1.0" ?>
<document type="product download">
<products>
<product cwr_sku="36384">
<description title="SPOT 2 Satellite GPS Messenger - Orange"/>
<manufacturer cwr_id="100122" name="SPOT" title="SPOT"/>
<rebate status="Y"/>
</product>

<product cwr_sku="41040">
<description title="SPOT Connect Smartphone Satellite Communicator"/>
<manufacturer cwr_id="100122" name="SPOT" title="SPOT"/>
<rebate status="Y"/>
</product>

<product cwr_sku="41299">
<description title="SPOT Floating Case"/>
<manufacturer cwr_id="100122" name="SPOT" title="SPOT"/>
<rebate status="Y"/>
</product>
</products>
</document>
Just change one or more of the <rebate status="Y"/> to <rebate status="N"/> to see how the code reacts differently.

- Bruce /*somdcomputerguy */  If you change the way you look at things, the things you look at change.

Share this post


Link to post
Share on other sites

Yes, this works. WTF?

Once again, thank you.

Share this post


Link to post
Share on other sites

"somdcomputerguy" this does not relate to the problem of "namspace" in xml (or I'm missing something).

I think "jdelaney" got it to work, I just don't understand why it works. As I have said before, I must learn more about working with "namespaces" in MSXML.DOM.

Share this post


Link to post
Share on other sites

 

(or I'm missing something)

No, you're not missing anything. My post didn't relate to your issue, and I knew it didn't. I was just trying to offer something that may have given you (or anyone else for that matter) some different insight or something. Good luck in resolving your issue though. :)


- Bruce /*somdcomputerguy */  If you change the way you look at things, the things you look at change.

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  
Followers 0