Sign in to follow this  
Followers 0
ProgrammingBrain

Help in XML File Editing using UDF

8 posts in this topic

i want to Edit an XML file using _XMLDOMWrapper.au3 UDF but i cant there is some thing wrong , i want to edit an value in specified root section in an XML file has many roots sections

for example :want to change value1 data in root1 section to "Data_False"

help :)

XML File

<?xml version="1.0" encoding="UTF8"?>
<root1>
    <value1>data1</value1>
    <value2>data2</value2>
    <value3>data3</value3>
    <value4>data4</value4>
</root1>
<root2>
        <value1>data1</value1>
    <value2>data2</value2>
</root2>

Code

#include <_XMLDOMWrapper.au3>
#include <Array.au3> 

$sXmlFile = @ScriptDir & "\XML_file.xml"
$sXPath = "/root1/value1"

_XMLFileOpen($sXmlFile)

For $n = 1 To 4
    $aRET = _XMLGetValue($sXPath)
    _ArrayDisplay($aRET, "Before")

    $sNewVal = String($aRET[1] = "Data_False")
    _XMLUpdateField($sXPath, $sNewVal)
    $aRET = _XMLGetValue($sXPath)
    _ArrayDisplay($aRET, "After")
Next

Share this post


Link to post
Share on other sites



An XML document is only allowed to have 1 root node.

You could try modifying the file be adding a proper root element.

<?xml version="1.0" encoding="UTF8"?>

<realRoot>
<root1>
    <value1>data1</value1>
    <value2>data2</value2>
    <value3>data3</value3>
    <value4>data4</value4>
</root1>
<root2>
        <value1>data1</value1>
    <value2>data2</value2>
</root2>
</realRoot>

"Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the universe trying to build bigger and better idiots. So far, the universe is winning."- Rick Cook

Share this post


Link to post
Share on other sites

An XML document is only allowed to have 1 root node.

You could try modifying the file be adding a proper root element.

<?xml version="1.0" encoding="UTF8"?>

<realRoot>
<root1>
    <value1>data1</value1>
    <value2>data2</value2>
    <value3>data3</value3>
    <value4>data4</value4>
</root1>
<root2>
    <value1>data1</value1>
    <value2>data2</value2>
</root2>
</realRoot>

but i saw some xml files like this

<?xml version="1.0" encoding="UTF8"?>

<root1>
    <value1>data1</value1>
    <value2>data2</value2>
    <value3>data3</value3>
    <value4>data4</value4>
</root1>
<root2>
        <value1>data1</value1>
    <value2>data2</value2>
</root2>

Share this post


Link to post
Share on other sites

but i saw some xml files like this

<?xml version="1.0" encoding="UTF8"?>

<root1>
    <value1>data1</value1>
    <value2>data2</value2>
    <value3>data3</value3>
    <value4>data4</value4>
</root1>
<root2>
        <value1>data1</value1>
    <value2>data2</value2>
</root2>

Really?

Can Internet Explorer open the XML file?

Share this post


Link to post
Share on other sites

Really?

Can Internet Explorer open the XML file?

no internet explorer cant open it but this xml model for an program config.xml

and this program can write data in root1 and root2 with out any problem ?

Share this post


Link to post
Share on other sites

The _XMLDOMWrapper UDF uses the MSXML API. It doesn't have any magic in it that allows it to use badly formatted XML.

What program is it that uses this bad XML for a config file?

:)


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

#7 ·  Posted (edited)

Your going to need to use some sort of bodge like this...

#include "_XMLDomWrapper.au3"
#Include <String.au3>


$XMLString = _ReturnXMLDataAsString("test.xml","root1")

_XMLLoadXML($XMLString)

$data = _XMLGetFirstValue("/root1/value1")

Msgbox(0,"",$data)


Func _ReturnXMLDataAsString($file,$root)

    Local $Str = FileRead($file)
    Local $aXMLData =  _StringBetween($Str,$root,$root)
    Local $XML = "<" & $root  & $aXMLData[0] &  $root & ">"
    Return $XML

EndFunc


Func _XMLGetFirstValue($node)

    Local $ret_val

    $ret_val = _XMLGetValue($node)
    If IsArray($ret_val) Then
        Return ($ret_val[1])
    Else
        Return SetError(1,3,0)
    EndIf

EndFunc
Edited by ChrisL

Share this post


Link to post
Share on other sites

Fixed :)

thanks guys for help :)

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