Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Processing an XML document 1

Status
Not open for further replies.

paulparky

Programmer
Oct 10, 2001
29
PA
I hope someone can help me with this as I've been scratchiing my head all day about it. I have an XML document loaded into a webpage and using ASP I am trying to extract the data from it. This goes well (if somewhat clumsily) until I reach the "RepairLines" section. I am trying to retrieve the data from within each "RepairLine" but am failing miserably.
The XML document looks like this
<ContainerData>
<JobType>Authorised</JobType>
<DepotCode>FLX</DepotCode>
<ContainerNo>TXLU8393021</ContainerNo>
<RecordNo>1</RecordNo>
<Client>BLU</Client>
<Model>MODEL1</Model>
<Size>20</Size>
<Type>REF</Type>
<ISOCODE>Test1</ISOCODE>
<SerialNumber>2300</SerialNumber>
<Manufacturer>sss</Manufacturer>
<CleanOnly>Y</CleanOnly>
<SMV></SMV>
<SSV></SSV>
<Sound-Indicator>N</Sound-Indicator>
<ACEP></ACEP>
<Warranty>Y</Warranty>
<Remarks>asdasdasdasdasdsdsaddasdasdasd</Remarks>
<SendEst>False</SendEst>
<EstimateDate>13-01-06-04-52-04</EstimateDate>
<RepairLines>
<RepairLine code="1">
<Location>BR4N</Location>
<Component>BCD</Component>
<Damage>BK</Damage>
<Repair>PC</Repair>
<Material>PM</Material>
<Length>12</Length>
<Width>10</Width>
<Quantity>1</Quantity>
<PartNumber></PartNumber>
<LabourHour>12</LabourHour>
<MaterialCost>2</MaterialCost>
<RepairComplete>N</RepairComplete>
</RepairLine>
<RepairLine code="2">
<Location>D</Location>
<Component>SGL</Component>
<Damage>CO</Damage>
<Repair></Repair>
<Material>PM</Material>
<Length>100</Length>
<Width>20</Width>
<Quantity>2</Quantity>
<PartNumber>14-00220-00</PartNumber>
<LabourHour>2</LabourHour>
<MaterialCost>2</MaterialCost>
<RepairComplete>False</RepairComplete>
</RepairLine>
</RepairLines>

I need to get the Location, Component, Damage etc. as individual items from the RepairLine but can only manage to get them in a string

The piece of code is
Set ObjNodes = objXMLdoc.documentElement.childnodes
for each strnode in objnodes
fname = strnode.NodeName
fdata = strnode.xml
if fname = "RepairLines" then
repcount = 0
set replist = objXMLdoc.getElementsByTagName("RepairLine")
for repcount = 0 to replist.length-1
response.Write("item " & replist.item(repcount).text & "<br>")
next
This gives me "item BR4N BCD BK PC PM 12 10 1 12 2 N" and "item D SGL CO PM 100 20 2 14-00220-00 2 2 False"

As you can probably tell I am new to processesing XML and flying by the seat of my pants at the moment.

Thanks in anticipation for any help/pointers you gurus can give.
 
If your guaranteed to always have the child nodes under RepairLine in the same order (and they are always there, even if empty) then you could reference them by their index under RepairLine like so:
Code:
Set replist = objXMLdoc.getElementsByTagName("RepairLine")
For repcount = 0 To replist.length-1
   Response.Write "Item Code: " & replist(repcount).Attributes(0) & "<br>"
   Response.Write "Location: " & replist(repcount).childNodes(0).Text & "<br>"
   Response.Write "Component: " & replist(repcount).childNodes(1).Text & "<br>"
   Response.Write "Damage: " & replist(repcount).childNodes(2).Text & "<br>"
   Response.Write "Repair: " & replist(repcount).childNodes(3).Text & "<br>"
   Response.Write "Material: " & replist(repcount).childNodes(4).Text & "<br>"
   '...etc
Next

Another option would be to use XPath expressions to get the nodes you want (more info here:
Here is an example I made based on your code above:
Code:
Dim objNodes, objXMLdoc
Set objXMLdoc = Server.CreateObject("Microsoft.XMLDOM")
objXMLdoc.Load Server.MapPath("test.xml")
If objXMLdoc.parseError.errorCode <> 0 Then
	Response.Write "Error: " & objXMLdoc.parseError
	Response.End
End If

Dim replist, rep_node
Set replist = objXMLdoc.SelectNodes("ContainerData/RepairLines/RepairLine")
For Each rep_node in replist
	Response.Write "Repair Code: " & rep_node.SelectSingleNode("@code").Text & "<br>"
	Response.Write "&nbsp;&nbsp;&nbsp;Location: " &  rep_node.SelectSingleNode("Location").Text & "<br>"
	Response.Write "&nbsp;&nbsp;&nbsp;Component: " &  rep_node.SelectSingleNode("Component").Text & "<br>"
	Response.Write "&nbsp;&nbsp;&nbsp;Damage: " &  rep_node.SelectSingleNode("Damage").Text & "<br>"
	Response.Write "&nbsp;&nbsp;&nbsp;Repair: " &  rep_node.SelectSingleNode("Repair").Text & "<br>"
	'...etc
	Response.Write "<br>"
Next

The first one may have some issues since I typed it on the fly. The second one should be perfectly fine, syntax wise, since I actually typed that one in an editor and created a test.xml file locally to play with.

Hope that covers what your looking for,
-T

barcode_1.gif
 
What a guy!! Thank you, you saved me from losing my hair altogether. Have a pint on me this weekend :eek:)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top