Jump to content
Sign in to follow this  
JimmyCollins

XML Parsing

Recommended Posts

JimmyCollins

Hi,

I am trying to change some attribute values in a large XML file. At the moment my code just changes the first value it finds in the file for some reason. Can anybody review my code and tell me if they spot the mistake?

The XML looks like this:

CODE

<customers>

<customerGroup name="supplies">

<acc1 accnt_id="33421145"/>

<acc2 accnt_id="54645533"/>

<acc3 accnt_id="78567868"/>

<acc4 accnt_id="79079077"/>

<acc5 accnt_id="89779867"/>

<acc6 accnt_id="78969868"/>

</customerGroup>

</customers>

I am trying to change all 'accnt_id' attribute to '00000000', but my code just changes the first one and finishes executing. Here is my code:

CODE

; Get the xml file

$filename = @ScriptDir & "\sample2.xml"

; Parse the result

$oXML = _CreateMSXMLObj()

If Not IsObj($oXML) Then

MsgBox(0, "_CreateMSXMLObj()", "ERROR!: Unable to create MSXML Object!!", 10)

Exit 1

EndIf

$oXML.async = False

$error = $oXML.Load($filename)

If Not $error Then

MsgBox(0,"Load XML", "An error occurred loading " & $filename, 10 )

Exit 1

EndIf

; Need to change the accnt_id attribute

$oXMLRoot = $oXML.documentElement

$oBckEndNode = $oXML.selectSingleNode("//customers")

$accountId = "00000000"

; For each app node in apps (change 'accnt_id' & 'e_mail')

For $oCurrentXmlNode In $oBckEndNode.childNodes

;-------------------------------------------------------------------------------------------------------------------------------

MsgBox(1, "Debug", "Changing Attributes")

$oAppBackendNode = $oCurrentXmlNode.firstchild

$oAppBackendNode.setAttribute ("accnt_id", $accountId)

; Save the xml file

$oXML.Save ($filename)

;-------------------------------------------------------------------------------------------------------------------------------

Next

Func _CreateMSXMLObj() ; Creates a MSXML instance depending on the version installed on the system

$xmlObj = ObjCreate("Msxml2.DOMdocument.6.0") ; Latest available, default in Vista

If IsObj($xmlObj) Then Return $xmlObj

$xmlObj = ObjCreate("Msxml2.DOMdocument.5.0") ; Office 2003

If IsObj($xmlObj) Then Return $xmlObj

$xmlObj = ObjCreate("Msxml2.DOMdocument.4.0")

If IsObj($xmlObj) Then Return $xmlObj

$xmlObj = ObjCreate("Msxml2.DOMdocument.3.0") ; XP and w2k3 server

If IsObj($xmlObj) Then Return $xmlObj

$xmlObj = ObjCreate("Msxml2.DOMdocument.2.6") ; Win98 ME...

If IsObj($xmlObj) Then Return $xmlObj

Return 0

EndFunc

If someone could help, that would be great. This drives my crazy <_<

Thanks,

JC

Share this post


Link to post
Share on other sites
PsaltyDS

Hi,

I am trying to change some attribute values in a large XML file. At the moment my code just changes the first value it finds in the file for some reason. Can anybody review my code and tell me if they spot the mistake?

I am trying to change all 'accnt_id' attribute to '00000000', but my code just changes the first one and finishes executing. Here is my code:

If someone could help, that would be great. This drives my crazy :)

It was doing exactly what you told it to do... try this change:

; For each app node in apps (change 'accnt_id' & 'e_mail')
For $oCurrentXmlNode In $oBckEndNode.childNodes ; This finds "customerGroup"
    For $oAppBackendNode In $oCurrentXmlNode.childNodes ; This finds the "acc*" entries
        $oAppBackendNode.setAttribute ("accnt_id", $accountId)
    Next
Next

; Save the xml file
$oXML.Save ($filename)

Works for me.

<_<


Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law

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  

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.