0

I'm using vbscript to write form data to an XML file:

Set xmlDoc = CreateObject("Microsoft.XMLDOM")
Set objRoot = xmlDoc.createElement("root")
xmlDoc.appendChild objRoot

Set objRecord = xmlDoc.createElement("document")
objRoot.appendChild objRecord

Set objElement=xmlDoc.createElement("field")
objElement.setAttribute "level", "system"
objElement.setAttribute "name", "Document Filename with Full Path"
objElement.setAttribute "value", "C:\out\test.pdf"
objrecord.appendChild(objElement)

Set objIntro = xmlDoc.createProcessingInstruction ("xml","version='1.0'")
xmlDoc.insertBefore objIntro,xmlDoc.childNodes(0)

Call ParseAndSave("C:\out\test.xml", xmlDoc)

Function ParseAndSave(filePath, xmlDoc)
set xmlWriter = CreateObject("MSXML2.MXXMLWriter")
set xmlReader = CreateObject("MSXML2.SAXXMLReader")
Set xmlStream = CreateObject("ADODB.STREAM")
xmlStream.Open
xmlStream.Charset = "UTF-8"

xmlWriter.output = xmlStream
xmlWriter.indent = True
xmlWriter.encoding = "UTF-8"

Set xmlReader.contentHandler = xmlWriter
Set xmlReader.DTDHandler = xmlWriter
Set xmlReader.errorHandler = xmlWriter
xmlReader.putProperty "http://xml.org/sax/properties/lexical-handler", xmlWriter
xmlReader.putProperty "http://xml.org/sax/properties/declaration-handler", xmlWriter

xmlReader.parse xmlDoc
xmlWriter.flush

xmlStream.SaveToFile filePath, 2

xmlStream.Close
Set xmlStream = Nothing
Set xmlWriter = Nothing
Set xmlReader = Nothing
End Function

It works, but the output not formatting like expect from an XML file:

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<root>
    <document>
        <field level="system" name="Document Filename with Full Path" value="C:\out\test.pdf"/>
    </document>
</root>

expected result. All new line aligned to left and between name and value got sapce

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<root>
<document>
<field level = "system" name = "Document Filename with Full Path" value = "C:\out\test.pdf"/>
</document>
</root>
6
  • You could try something like this with msxml stackoverflow.com/a/3500085/1684418 that is use the createAttribute() function Commented Dec 1, 2016 at 3:32
  • Thanks for command, looks not my issues. I need add a space being and after = (like this got spaces level = "system", but my outcome no space like level="system") Commented Dec 1, 2016 at 4:13
  • Why would you even want to do that? Indention visualizes the structure of the XML for human readers, and it's ignored by automated processing anyway. And the additional spaces around the = do not contribute to readability either. Commented Dec 1, 2016 at 11:17
  • it's a integrate between existing DMS and new scanning system. I need use xml file to upload the source file to DMS and found the DMS cannot upload without the spaces around = , if use normal xml format with tag for the child element also not work. Commented Dec 2, 2016 at 3:48
  • You're entrusting your documents to a DMS that can't even get data import right? Wow. Commented Dec 2, 2016 at 9:52

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.