I have a downloaded xml file with several main nodes repeated, how do I separate these nodes into different xml files, using vb6.
This is an example of the xml I'm working with:
<produto>
<item>
<codigo>001</codigo>
<qtde>1</qtde>
</item>
</produto>
<produto>
<item>
<codigo>002</codigo>
<qtde>33</qtde>
</item>
</produto>
<produto>
<item>
<codigo>004</codigo>
<qtde>10</qtde>
</item>
</produto>
<produto>
<item>
<codigo>005</codigo>
<qtde>6</qtde>
</item>
</produto>
I tried using the following code:
Dim xmldoc As New MSXML2.DOMDocument
Dim noList As MSXML2.IXMLDOMNodeList
Dim node As MSXML2.IXMLDOMNode
xmldoc.loadXML("C:\\test.xml")
Set noList = xmldoc.getElementsByTagName("produto")
For i = 0 To noList.length - 1
'
Set node = noList.Item(i)
xmldoc.loadXML node.nodeValue
strFile = "Arq_" & i & ".xml"
node.Save strFile
'
Next
But there was an error on the line: Set noList = xmldoc.getElementsByTagName("produto")
As already mentioned, this is not a well-formed XML file, missing a root node. Your XML currently has 4 route nodes
produto.So insert a root node, e.g.
I also personally prefer
.selectNodes()over.getElementsByTagName(). It uses a XPath expression, which gives you more control over the selected elements. Not in this particular example, but in general. E.g. for the above example