Jump to content

writing to xml file


Recommended Posts

I'm using erifash's UDF for parsing XML. It works great for reading xml files, but not so much for writing to them without needing to start from scratch. I want to replace text between two meta tags in an xml file, but it seems to always add the text I want to replace (along with the tags) to the bottom of the file.

#include <xmlparser.au3>
$xml = _XMLLoad("C:\file.xml")
_XMLSet($xml, "file", "C:\test\oldlocation.file"); you could manually edit the file to say oldlocation if you think writing this twice is improper
_XMLSet($xml, "file", "C:\test\newlocation.file")
_XMLSave($xml, "C:\file.xml")
MsgBox(0, "location", $xml)

If you run this a couple of times you'll see what I mean. It's just not replacing the tag, only appending to the end of the file.

Short of just re-writing the file I want to modify, I was hoping there was a cleaner way. Any help would be greatly appreciated.

Link to comment
Share on other sites

I'm using erifash's UDF for parsing XML. It works great for reading xml files, but not so much for writing to them without needing to start from scratch. I want to replace text between two meta tags in an xml file, but it seems to always add the text I want to replace (along with the tags) to the bottom of the file.

#include <xmlparser.au3>
$xml = _XMLLoad("C:\file.xml")
_XMLSet($xml, "file", "C:\test\oldlocation.file"); you could manually edit the file to say oldlocation if you think writing this twice is improper
_XMLSet($xml, "file", "C:\test\newlocation.file")
_XMLSave($xml, "C:\file.xml")
MsgBox(0, "location", $xml)

If you run this a couple of times you'll see what I mean. It's just not replacing the tag, only appending to the end of the file.

Short of just re-writing the file I want to modify, I was hoping there was a cleaner way. Any help would be greatly appreciated.

That UDF hasn't been maintained since November of 2005. You should probably try out the _XMLDOMWrapper.au3 UDF by eltorro.

If you need more help, post a short sample XML file so we can see what you are trying to edit.

:)

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

I was avoiding eltorro's as it seems to require that you use the beta version of AutoIt to work properly. I was hoping that erifash's was functional, but maybe I was just using it wrong.

The xml code looks something like:

<version foo>
<other>misc</other>
<file>C:\test\oldlocation.file</file>
<blah>data</blah>

Is there another method I could use just to replace the data between <file> and </file>?

Link to comment
Share on other sites

I was avoiding eltorro's as it seems to require that you use the beta version of AutoIt to work properly. I was hoping that erifash's was functional, but maybe I was just using it wrong.

The xml code looks something like:

<version foo>
<other>misc</other>
<file>C:\test\oldlocation.file</file>
<blah>data</blah>

Is there another method I could use just to replace the data between <file> and </file>?

If you want to do it with string manipulation:

#include <String.au3>

$sNewData = "C:\test\newlocation.file"
$sXMLFile = "C:\file.xml"

$sXML = FileRead($sXMLFile)
$avData = _StringBetween($sXML, "<file>", "</file>")
$sXML = StringReplace($sXML, "<file>" & $avData[0] & "</file>", "<file>" & $sNewData & "</file>")
$hXMLFile = FileOpen($sXMLFile, 2) ; 2 = Overwrite
FileWrite($hXMLFile, $sXML)

We'll leave the RegExp for another day...

:)

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

If you want to do it with string manipulation:

#include <String.au3>

$sNewData = "C:\test\newlocation.file"
$sXMLFile = "C:\file.xml"

$sXML = FileRead($sXMLFile)
$avData = _StringBetween($sXML, "<file>", "</file>")
$sXML = StringReplace($sXML, "<file>" & $avData[0] & "</file>", "<file>" & $sNewData & "</file>")
$hXMLFile = FileOpen($sXMLFile, 2) ; 2 = Overwrite
FileWrite($hXMLFile, $sXML)

We'll leave the RegExp for another day...

-_-

Strings and regular expressions are not my strong point, but that code makes perfect sense. Thank you!

I've never used it with beta. Works fine with 3.2.10.0.

I will have to try this, too. When I saw in the source "These funtions(sic) require BETA as they need support for COM," I assumed.
Link to comment
Share on other sites

Without xpath support how would I set the required hex colors easily...

<colors>

<color name="Red" hex="0x000000"/>

<color name="Blue" hex="0x000000"/>

<color name="Green" hex="0x000000"/>

</colors>

With xpath support I do this:

#include <_XMLDomWrapper.au3>
#include 

$sXMLFile = "colors.xml"

$result = _XMLFileOpen($sXMLFile)
If $result = 0 Then Exit

$path = "//colors"

;Retrieve all subnodes of "Keys" (used for count)
$nodesArray = _XMLGetChildNodes($path)

For $X = 1 to $nodesArray[0]
    ConsoleWrite(_XMLGetAttrib($path & "/color[" & $X & "]", "name") & @CRLF)
Next

;Set attribute for specific element (in the example we have 3 elements)
_XMLSetAttrib($path & "/color[@name='Red']", "hex", "0xFF0000")
_XMLSetAttrib($path & "/color[@name='Blue']", "hex", "0x0000FF")
_XMLSetAttrib($path & "/color[@name='Green']", "hex", "0x00FF00")
Link to comment
Share on other sites

I will have to try this, too. When I saw in the source "These funtions(sic) require BETA as they need support for COM," I assumed.

I think that is an OLD note that referred to Beta back in the 3.1.x days before COM Object support was added to AutoIt (summer 2006). I don't think it is meant to imply you still need Beta with 3.2.10.0 Prod today.

:)

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

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