Jump to content

How to make freshly baked XML nicely aligned ?


Iczer
 Share

Recommended Posts

As title says - How to make freshly baked XML nicely aligned? Without using Regexp, or anything of sort as it (nice align) would be ruined after first write to XML-file.

$oXML_PatternNames = ObjCreate("Msxml2.DOMDocument.6.0")
$oXMLDeclaration = $oXML_PatternNames.createProcessingInstruction ("xml", "version=""1.0"" encoding=""UTF-8""")
$oXML_PatternNames.appendChild ($oXMLDeclaration)
$oRoot = $oXML_PatternNames.createElement ("ListDefaults")
$oRoot.setAttribute("xmlns:xsi","http://www.w3.org/2001/XMLSchema-instance")
$oRoot.setAttribute("xmlns:xsd","http://www.w3.org/2001/XMLSchema")
$oRoot.setAttribute("Name","")
$oRoot.setAttribute("Key","0")
$oRoot.setAttribute("Value","txt")
$oXML_PatternNames.appendChild($oRoot)

For $i = 1 To 7
    ConsoleWrite ( $i &@crlf)
    $oChild = $oXML_PatternNames.createElement ("Pattern")
    $oChild.CreateAttribute("Name")
    $oChild.SetAttribute("Name","asdasdasd")
    $oChild.CreateAttribute("Key")
    $oChild.SetAttribute("Key","78")
    $oChild.CreateAttribute("Value")
    $oChild.SetAttribute("Value","txt")
    $oRoot.AppendChild($oChild)
Next
ConsoleWrite ( "RAW -------------------------------------------" &@crlf)
ConsoleWrite ( $oXML_PatternNames.xml&@crlf)

$tXML_PatternNames = StringRegExpReplace($oXML_PatternNames.xml,"><",">"&@crlf&"<")
$tXML_PatternNames = StringRegExpReplace($tXML_PatternNames,"<Pattern","  <Pattern")
$oXML_PatternNames = ObjCreate("Msxml2.DOMDocument.6.0")
$oXML_PatternNames.LoadXML($tXML_PatternNames)
ConsoleWrite ( "REGEXP ----------------------------------------" &@crlf)
ConsoleWrite ( $oXML_PatternNames.xml&@crlf)

$oChild = $oXML_PatternNames.createElement ("Pattern")
$oChild.CreateAttribute("Name")
$oChild.SetAttribute("Name","fffffffffff")
$oChild.CreateAttribute("Key")
$oChild.SetAttribute("Key","77")
$oChild.CreateAttribute("Value")
$oChild.SetAttribute("Value","txq")
$oXML_PatternNames.selectSingleNode("ListDefaults").AppendChild($oChild)

ConsoleWrite ( "ADD some element ------------------------------" &@crlf)
ConsoleWrite ( $oXML_PatternNames.xml&@crlf)

On a side note - why this script crash in 3.3.8.1, but work fine on 3.3.10.2 ?

test_421.au3 (15) : ==> The requested action with this object has failed.:
1
$oChild.CreateAttribute("Name")
$oChild.CreateAttribute("Name")^ ERROR
Link to comment
Share on other sites

Hey Iczer,

It might be easier for other people to understand if you explain what you are trying to achieve first :)

I tried compiling your script but i'm getting this error:

C:\Users\Kev\Desktop\test.au3 (27) : ==> The requested action with this object has failed.:
$oChild.CreateAttribute("Name")
$oChild.CreateAttribute("Name")^ ERROR
Link to comment
Share on other sites

Maybe like this:

;...

ConsoleWrite("RAW -------------------------------------------" & @CRLF)
ConsoleWrite($oXML_PatternNames.xml & @CRLF)


Local $oReader = ObjCreate("MSXML2.SAXXMLReader")
Local $oWriter = ObjCreate("MSXML2.MXXMLWriter")

$oWriter.indent = True ; this is it

$oReader.contentHandler = $oWriter
$oReader.parse($oXML_PatternNames.xml)

$sFormatted = $oWriter.output

ConsoleWrite("FORMATTED -------------------------------------------" & @CRLF)
ConsoleWrite($sFormatted & @CRLF)

♡♡♡

.

eMyvnE

Link to comment
Share on other sites

 

Hey Iczer,

It might be easier for other people to understand if you explain what you are trying to achieve first :)

 

all i want is to make xml-file maintain human readable form after write in it. Preferably automatically. :poke:

I tried compiling your script but i'm getting this error:
C:\Users\Kev\Desktop\test.au3 (27) : ==> The requested action with this object has failed.:
$oChild.CreateAttribute("Name")
$oChild.CreateAttribute("Name")^ ERROR

 

yeah, as it stated in op topic

Edited by Iczer
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...