Jump to content

Parse XML files?


holsy

Recommended Posts

Can anyone give me some pointers on how to parse xml files using asp? I have tried numerous examples searching google but can't get it to work. I have not performed an xml read before. Here is the xml file structure (note: this xml file has no root element).

<?xml version="1.0" encoding="utf-8"?><item><supplier_id>2006</supplier_id><drop_ship_fee>3.00</drop_ship_fee><supplier_name>Beethoven</supplier_name><product_id>4185456</product_id><product_sku>340</product_sku><item_last_update>2011-12-31 15:07:39.0</item_last_update><item_discontinued_date></item_discontinued_date><categories><category><category_id>7912</category_id><category_name>Catalog</category_name></category><category><category_id>10184</category_id><category_name>Kids, baby & toy</category_name></category><category><category_id>10196</category_id><category_name>Toy</category_name></category><category><category_id>10212</category_id><category_name>Puzzles</category_name></category></categories><attributes></attributes><folder_paths><folder_path><value><folder_id>1151651</folder_id><folder_name>Feb03_2012</folder_name></value><value><folder_id>1151664</folder_id><folder_name>Data Export - 2012-02-03 53</folder_name></value></folder_path></folder_paths><is_customized>0</is_customized></item><item><supplier_id>2006</supplier_id><drop_ship_fee>5.00</drop_ship_fee><supplier_name>Beethoven</supplier_name><product_id>4185486</product_id><product_sku>343</product_sku><item_last_update>2011-12-31 16:07:39.0</item_last_update><item_discontinued_date></item_discontinued_date><categories><category><category_id>4912</category_id><category_name>Catalog</category_name></category><category><category_id>10184</category_id><category_name>Kids, baby & toy</category_name></category><category><category_id>10196</category_id><category_name>Toy</category_name></category><category><category_id>10212</category_id><category_name>Puzzles</category_name></category></categories><attributes></attributes><folder_paths><folder_path><value><folder_id>1151651</folder_id><folder_name>Feb03_2012</folder_name></value><value><folder_id>1151664</folder_id><folder_name>Data Export - 2012-02-03 53</folder_name></value></folder_path></folder_paths><is_customized>0</is_customized></item>

Here is the asp code. I have tried these example but can't get it to work:

<%'Set the XML ObjectSet objXML = Server.CreateObject("Microsoft.XMLDOM")'Set Asynchoronous = falseobjXML.async = False'Load the XML file.'User Server.MapPath method is the XML is located in your site.'Else you can use the absolute path.objXML.Load (DestinationPath & "\" & strDatasource)'If there is any errors pasring the file the notifyIf objXML.parseError.errorCode <> 0 ThenResponse.Write "Error Parsing XML"Response.Write  "Rason :" & objXML.parseError.reason & "Error Line: " & objXML.parseError.lineEnd If'Get ALL the Elements by the tag name bookSet myOBJ = objXML.getElementsByTagName("item")'Now Iterate through the List and DisplayFor i = 0 to (myOBJ.Length-1)	Response.Write "supplier_id: " & books.item(i).childNodes(0).text  & "<br/>"	Response.Write "drop_ship_fee: " & books.item(i).childNodes(1).text & "<br/>"	Response.Write "supplier_name: " & books.item(i).childNodes(2).text & "<br/>"Next%>

The other example is:

Dim objxmlSet objxml = Server.CreateObject("Microsoft.XMLDOM")objxml.async = Falseobjxml.load (DestinationPath & "\" & strDatasource) set ElemProperty = objxml.getElementsByTagName("item")set ElemSupplier_id = objxml.getElementsByTagName("item/supplier_id")set ElemDrop_ship_fee = objxml.getElementsByTagName("item/drop_ship_fee")set ElemSupplier_name = objxml.getElementsByTagName("item/supplier_name")  For i=0 To (ElemProperty.length -1)  Response.Write " Supplier ID = "  Response.Write(ElemSupplier_id.item(i).Text) & "<br>"  Response.Write " Drop Ship Fee = "  Response.Write(ElemDrop_ship_fee.item(i).Text) & "<br>"  Response.Write " Supplier Name = "  Response.Write(ElemSupplier_name.item(i).Text) & "<br>"nextSet objxml = Nothing

What am I doing wrong?

Link to comment
Share on other sites

What happens when you run the code? Are DestinationPath and strDatasource defined? It may be a problem if your XML is not well-formed, XML parsers like these will usually bail out if the XML is not well-formed. Being well-formed includes having a root element.

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...