Hi,
I have a problem.
I need to read some data from a text file and need to put it into XML file using VB6.

I have the follwoing input from the text file:

Suite id="ADD"
description="Add Validation Suite"
TestCase id="ATC_V_Add001"
and so on..........

and I want the following format in XML file. I have to update the XML file with the following info:

<?xml-stylesheet type="text/xsl" href="TestSuite.xsl"?>
<LLD>
<Cycle>
<Suite id="ADD" description="Add Validation Suite">
<TestCase id="ATC_V_Add001" description="Verify the Rulecode ADD001 with Address Overlap in the same Zone or City exception">
<LinkPVID>54221487</LinkPVID>

<Results>Exception ADD001 is generated see details in Exception info section</Results>

<StepTable id="StartUpApplication" description="To launch the application" />

</TestCase>
</Suite>
</Cycle>
</LLD>

Dani AI

Generated

As suggested, use MSXML from VB6. To pick the Suite element that has a particular id the cleanest option is XPath — e.g. select the node with //Suite[@id='ADD']. Below is a practical pattern: load the file, enable XPath, locate the Suite node, create a TestCase (with LinkPVID, Results and a StepTable), append it, and save.

Dim xmlDoc As New MSXML2.DOMDocument60
Dim suiteNode As IXMLDOMNode
Dim newTC As IXMLDOMElement
Dim linkNode As IXMLDOMElement, resultsNode As IXMLDOMElement, stepTable As IXMLDOMElement

xmlDoc.async = False
If Not xmlDoc.Load("C:\Test.xml") Then
  MsgBox "XML load failed: " & xmlDoc.parseError.reason
  Exit Sub
End If

xmlDoc.setProperty "SelectionLanguage", "XPath"
Set suiteNode = xmlDoc.selectSingleNode("//Suite[@id='ADD']")

If suiteNode Is Nothing Then
  MsgBox "Suite with id='ADD' not found."
  Exit Sub
End If

Set newTC = xmlDoc.createElement("TestCase")
newTC.setAttribute "id", "ATC_V_Add001"
newTC.setAttribute "description", "Verify the Rulecode ADD001 with Address Overlap in the same Zone or City exception"

Set linkNode = xmlDoc.createElement("LinkPVID")
linkNode.text = "54221487"
newTC.appendChild linkNode

Set resultsNode = xmlDoc.createElement("Results")
resultsNode.text = "Exception ADD001 is generated see details in Exception info section"
newTC.appendChild resultsNode

Set stepTable = xmlDoc.createElement("StepTable")
stepTable.setAttribute "id", "StartUpApplication"
stepTable.setAttribute "description", "To launch the application"
newTC.appendChild stepTable

suiteNode.appendChild newTC
xmlDoc.Save "C:\Test.xml"

Troubleshooting notes: call setProperty "SelectionLanguage","XPath" before selectSingleNode. If the document uses a default namespace, declare it with SelectionNamespaces and use a prefix (for example xmlns:ns='URI' and //ns:Suite[@id='ADD']). Check xmlDoc.parseError.reason when Load fails. Use createTextNode or the .text property to ensure special characters are escaped. The getElementsByTagName + Attributes approach shown earlier by works too, but XPath is shorter and less error-prone when you need to match on an attribute value.

Recommended Answers

All 4 Replies

Hi,
Could you tell me how to select a node having a attribute. In the example

<Suite id="ADD" description="Add Validation Suite">
How to select the Suite node using the "id' value.

Thanks,
karthik

This Code select the ID Node. Save your xml in C:\Test.xml or Change the Name in Load () function.
I am not correct exactly. but it will do.

Dim xDOM As DOMDocument
   Set xDOM = New DOMDocument
   
   Dim xRoot As IXMLDOMElement
   Dim xNodeList As IXMLDOMNodeList
   Dim xNM As IXMLDOMNamedNodeMap
   Dim xID As IXMLDOMNode
   xDOM.Load "c:\test.xml"  ' Or your File Name
   
   Set xRoot = xDOM.documentElement
   Set xNodeList = xRoot.getElementsByTagName("Suite")
   
   If (Not xNodeList Is Nothing) Then
      Set xNM = xNodeList(0).Attributes
      Set xID = xNM.getNamedItem("id")
      MsgBox iM.Text
   End If

Sorry, i forgot to mention my exact requirement
i need to select the node with the value of id

Eg:To select the "Suite" node with "id "value="ADD" and need to add some more node under it

Thanks,
Karthik

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.