Jump to content
Sign in to follow this  
Lazycat

Creating XML declaration

Recommended Posts

Lazycat

I have one program that keep it's settings in the XML file. This is semi-valid file, because it's absent declaration, though file contain symbols that prevent MSXML parser to parse it. So for correct parsing I need to apply declaration with needed encoding to the loaded file.

Only examples I googled was NET examples. NET allow create declaration with the CreateXmlDeclaration and apply it to loaded document. How to make this in usual way?

Of course, I can merge declaration string with file content and write it to new file, but maybe exists right way to do it?

Share this post


Link to post
Share on other sites
Valik

The XML declaration is just a processing instruction. MSXML has a method for creating a processing instruction.

Share this post


Link to post
Share on other sites
Lazycat

Thanks, exactly!

Anyway, loading with Load method not works, so I used LoadXML instead - seems it's load any well formated data. Below is code for those who interested:

$oXml = ObjCreate("Msxml2.DOMdocument.3.0")
$oXml.LoadXML(FileRead("servercfg.xml", FileGetSize("servercfg.xml")))
$oXmlRoot = $oXml.documentElement
$oXmlDecl = $oXml.createProcessingInstruction("xml", 'version="1.0" encoding="UTF-8"')
$oXml.InsertBefore($oXmlDecl, $oXmlRoot)
$oXml.Save("servercfg_new.xml")

Share this post


Link to post
Share on other sites
webmedic

I'm curious then with the example, I know nothing about using whatever you did to lood the msxml, but if you wanted to parse an xml file and use it for a config file instead of say an ini file what commands would you use to iterate through the fields and get the data out of an xml file?

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  

×