Jump to content

Help in XML File Editing using UDF


Recommended Posts

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
Link to comment
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

Link to comment
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>
Link to comment
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?

Link to comment
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
Link to comment
Share on other sites

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
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...