Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a way to control which tags get collapsed/self-closed during serialization with MSXML6?

I create a MSXML6 DOM document and during serialization I want to control how empty elements are serialized:

  1. <tag></tag>

  2. <tag/>

This answer describes a solution for C#, but I'm looking for something possible with the ActiveX interface of MSXML. (For VB6 or some scripting language)

like image 948
Daniel Rikowski Avatar asked Sep 14 '25 23:09

Daniel Rikowski


1 Answers

This is incredibly messy, but I've discovered that if you use the createElement method on a MSXML document you get (for some reason) an xml element that serailizes to the <tag /> format, and so you can force elements to serailize like this by replacing them with elements you create with the same name:

<!-- Contents of c:\xml.xml -->
<xml>
    <element></element>
</xml>

In Javascript (but easy to convert to VbScript hopefully)

objXML = new ActiveXObject("MSXML2.DOMDocument.4.0");
objXML.load("c:\\xml.xml");

var xmlElement = objXML.childNodes[1];

var newElement = objXML.createElement(xmlElement.childNodes[0].tagName);
xmlElement.replaceChild(newElement, xmlElement.childNodes[0]);

Conversely, you can force unexpanded <tag /> elements to expand out by setting the text property to be "":

newElement.text = "";

Hope this helps - I know this is really really horrible, but the chances are the fact that you need to be able to do this in the first place is horrible enough already so this extra horribleness wont make much difference! :-p

like image 57
Justin Avatar answered Sep 17 '25 18:09

Justin