Jump to content

Recommended Posts

Posted

I have an XML document with multiple nodes called "Entity". I would like to create individual xml documents for each Entity.  I am using the addin XML_1.1.1.13 to read the xml file but I can't seem to figure out how to save out the individual entities.

I am successfully processing the XML file using the snippet below. Do I need to create a new XMLDoc and copy the nodes over one at a time?

 

$oXmlDoc = _XML_CreateDOMDocument(Default)
    _XML_Load($oXmlDoc,$sFileXML)  ;<== ENTER XML FILE PATH HERE

    ;Get number of Property nodes
    $oEntities= _XML_SelectNodes($oXmlDoc,"//Entity")

    For $oEntity In $oEntities
        Save Entity
    Next

 

Posted (edited)

You could read the file and do a stringsplit and then write the contents into those other files.

Edited by careca
  Reveal hidden contents

IUIAutomation - Topic with framework and examples

Au3Record.exe

Posted

$STR_ENTIRESPLIT

Signature beginning:
Please remember: "AutoIt"..... *  Wondering who uses AutoIt and what it can be used for ? * Forum Rules *
ADO.au3 UDF * POP3.au3 UDF * XML.au3 UDF * IE on Windows 11 * How to ask ChatGPT for AutoIt Codefor other useful stuff click the following button:

  Reveal hidden contents

Signature last update: 2023-04-24

Posted (edited)

You could also parse out the <Entity> nodes using a regular expression.

#include <Constants.au3>

example()

Func example()
    Const $XML_FILE  = "walkaround.xml" ;<-- Point to your xml file

    Local $sXmlData  = FileRead($XML_FILE)
    Local $aEntities = StringRegExp($sXmlData, "(?s)(<Entity.*?</Entity>)", $STR_REGEXPARRAYGLOBALMATCH)

    FileDelete("~xml_file_*")
    For $i = 0 To UBound($aEntities) - 1
        FileWrite(StringFormat("~xml_file_%02i.xml", $i), $aEntities[$i])
    Next
EndFunc

 

Edited by TheXman
Posted (edited)
;=============================================================================
#Region ;Includes
#RequireAdmin
;=============================================================================
#include <String.au3> ;This for _StringBetween
;=============================================================================
#EndRegion ;Includes
;=============================================================================
Global $StrBt
Global $FO = FileOpen('walkaround.xml')
Global $FR = FileRead($FO)
FileClose($FO)
$StrBt = _StringBetween($FR, '<Entity ', '</Entity>')
If Not @error Then
For $i = 0 To UBound($StrBt)-1
$FO = FileOpen(@ScriptDir&'\tmp\'& $i&'.txt', 10)
FileWrite($FO, $StrBt[$i])
FileClose($FO)
Next
MsgBox(64 + 262144, '', 'Done')
EndIf

 

Edited by careca
  Reveal hidden contents

IUIAutomation - Topic with framework and examples

Au3Record.exe

Posted
  On 12/17/2018 at 2:57 PM, jdelaney said:

Did you try as I suggested?  .xml on a node outputs the xml relative to that node.   throw it into a consoleoutput

Expand  

I tried but the files were 0bytes long.

For $oEntity In $oEntities
         $sString = ""
         filewrite("c:\temp\test"& $cnt & ".xml", $oEntity)
         $cnt = $cnt+1
     Next

 

Posted

The StringBetween approach works (with some very minor adjustments to output formatting to maintain correct XML formating). I am sure the regular expression approach would work as well but I have not used to using regular expressions much.

It would have been nice if filewrite( filename, $oEntity) would have worked but as I indicated, that only output 0 bytes.

Posted

In the remarks of Filewrite:

"The file must be opened in write mode or the FileWrite() command will fail."

Maybe it has something to do with that.

Try opening using flag 9 or 10 and then write and then close.

  Reveal hidden contents

IUIAutomation - Topic with framework and examples

Au3Record.exe

Posted (edited)
  On 12/17/2018 at 3:57 PM, ijourneaux said:

It would have been nice if filewrite( filename, $oEntity) would have worked but as I indicated, that only output 0 bytes.

Expand  

This works for me.  It uses the .xml property that @jdelaney was referring to.

#include <xml.au3>  ;<-- Change to your location

example()

Func example()
    Const $XML_FILE  = "walkaround.xml" ;<-- Point to your xml file

    Local $oXmlDoc, $oNode
    Local $iNodeCount = 0

    ;Load XML file
    $oXmlDoc = _XML_CreateDOMDocument()
    $oXmlDoc = _XML_Load($oXmlDoc, $XML_FILE)

    ;Get number of Entity nodes
    $iNodeCount = _XML_GetNodesCount($oXmlDoc, "//Entity")

    ;Write each Entity node to a separate file
    FileDelete("~xml_file_*")
    For $i = 1 To $iNodeCount
        $oNode = _XML_SelectSingleNode($oXmlDoc, StringFormat("//Entity[%i]", $i))
        FileWrite(StringFormat("~xml_file_%02i.xml", $i), $oNode.xml)
    Next
EndFunc

 

Edited by TheXman
Posted

Unfortunately it doesn't quite work the way I need. When I use the approach proposed by TheXMan, I end up losing the <Entities> node that wraps all of the Entity nodes. 
 

<?xml version="1.0"?>
<Entities>
...
</Entities>

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
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...