Solved

Parsing XML in ASP 3.0

Posted on 2008-10-20
4
405 Views
Last Modified: 2013-11-18
here is the sample XML file:

<?xml version="1.0" ?>
<dto>
  <address>
    <string>17 Palm Street Apple 2</string>
    <string>17 Palm Street Apple 2</string>
  </address>
  <city>
    <string>Cainta</string>
    <string>Cainta</string>
  </city>
</dto>

On this code:
  Dim objXMLDom
  Set objXMLDOM = Server.CreateObject("Microsoft.XMLDOM")
      
  objXMLDOM.async = False
  objXMLDom.load(Server.MapPath("sample.xml"))

  Dim objChild
  for each objChild in objXMLDOM.documentElement.childNodes
    Response.write(objChild.nodename)
    Response.write(": ")
    response.write(objChild.text & "<BR>")
  next

  Set objXMLDom = Nothing

OUTPUT:
address: 17 Palm Street Apple 2 17 Palm Street Apple 2
city: Cainta Cainta

I need to split the data into two, i data is: "17 Palm Street Apple 2" and another is "17 Palm Street Apple 2"

Hope you can help me.. thanks gurus!

0
Comment
Question by:jgsimbulan
4 Comments
 
LVL 16

Accepted Solution

by:
Gyanendra Singh earned 250 total points
ID: 22764555
0
 

Author Comment

by:jgsimbulan
ID: 22764692
on the URL you gave:
http://ubk.co.in/script-blog/index.php/parsing-xml-file-in-asp/

below is my code:
Set objXML = CreateObject("Microsoft.XMLDOM")

'Set Asynchoronous = false
objXML.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(Server.MapPath("sample.xml"))

'If there is any errors pasring the file the notify

If objXML.parseError.errorCode = 0 Then

      Response.Write "Error Parsing XML "

      Response.Write  "Rason : " & objXML.parseError.reason & " Error Line: " & objXML.parseError.line

End If

'Get ALL the Elements by the tag name book

Set books = objXML.getElementsByTagName("Address")

'Now Iterate through the List and Display

 For i = 0 to (books.Length-1)
       Response.Write books.item(i).childNodes(0).text
      
Next


ERROR:
Error Parsing XML Rason : Error Line: 0

below is the XML file;
<?xml version="1.0" ?>
<dto>
  <address>
    <string>17 Palm Street Apple 2</string>
    <string>17 Palm Street Apple 21asas</string>
  </address>
  <city>
    <string>Cainta</string>
    <string>Cainta</string>
  </city>
  <cpc>
    <string>U1A</string>
    <string>U1A</string>
  </cpc>
  <firstName>
    <string>Ana</string>
    <string>Ana</string>
  </firstName>
  <lastFourDigits>
    <string>7777</string>
    <string>7777</string>
  </lastFourDigits>
  <lastName>
    <string>Roces</string>
    <string>Roces</string>
  </lastName>
  <masterAccntNmbr>
    <string>4111111111111111</string>
    <string>4111111111111111</string>
  </masterAccntNmbr>
  <pointsAvail>
    <double>100000.0</double>
    <double>100000.0</double>
  </pointsAvail>
  <pointsRedeemedMostRecent>
    <double>50000.0</double>
    <double>50000.0</double>
  </pointsRedeemedMostRecent>
  <pointsRedeemedSecondRecent>
    <double>5000.0</double>
    <double>5000.0</double>
  </pointsRedeemedSecondRecent>
  <pointsRedeemedThirdRecent>
    <double>4000.0</double>
    <double>4000.0</double>
  </pointsRedeemedThirdRecent>
  <redemptionDateMostRecent>
    <gregorian-calendar>
      <time>1224558732568</time>
      <timezone>GMT</timezone>
    </gregorian-calendar>
    <gregorian-calendar>
      <time>1224558732568</time>
      <timezone>GMT</timezone>
    </gregorian-calendar>
  </redemptionDateMostRecent>
  <redemptionDateSecondRecent>
    <gregorian-calendar>
      <time>1224558732568</time>
      <timezone>GMT</timezone>
    </gregorian-calendar>
    <gregorian-calendar>
      <time>1224558732568</time>
      <timezone>GMT</timezone>
    </gregorian-calendar>
  </redemptionDateSecondRecent>
  <redemptionDateThirdRecent>
    <gregorian-calendar>
      <time>1224558732568</time>
      <timezone>GMT</timezone>
    </gregorian-calendar>
    <gregorian-calendar>
      <time>1224558732568</time>
      <timezone>GMT</timezone>
    </gregorian-calendar>
  </redemptionDateThirdRecent>
  <state>
    <string>Rizal</string>
    <string>Rizal</string>
  </state>
  <zipCode>
    <string>3023</string>
    <string>3023</string>
  </zipCode>
  <size>2</size>
  <errorCode>0</errorCode>
</dto>

Need help still...
0
 
LVL 1

Assisted Solution

by:JeffHand
JeffHand earned 250 total points
ID: 23853906
I don't know whether you are still looking for an answer to this, but being intrigued by all sorts of XML/XSLT issues, I took a quick pass at this. If I understood your question, you are looking to get the text of the two <string> elements to display on separate lines rather than all on one line as is being output by the code snippet you've shown.

You're actually very close to the what you need. It's a matter of child elements vs. "grandchild" elements. Your "for each" loop is getting first the <address> element and then the <city> element. The text property of each of these elements is the sum (concatenation) of all text regardless of element tags - this accounts for your current results.

To deal with the two <string> nodes individually, simply add a nested "for each" loop. For example, the following revised code produces the output shown beneath it...

Dim objChild
for each objChild in objXMLDOM.documentElement.childNodes
    dim oGChild
    for each oGChild in objChild.childNodes
        Response.write(objChild.nodename)
        Response.write(": ")
        response.write(oGChild.text & "<BR>")
    next
next

OUTPUT:
address: 17 Palm Street Apple 2
address: 17 Palm Street Apple 2
city: Cainta
city: Cainta

If you don't need to the node name in front of each line, simply modify where the various response.write statements go. Here's another example...

Dim objChild
for each objChild in objXMLDOM.documentElement.childNodes
    Response.write(objChild.nodename)
    Response.write(":<BR>")
    dim oGChild
    for each oGChild in objChild.childNodes
        response.write(oGChild.text & "<BR>")
    next
next

OUTPUT:
address:
17 Palm Street Apple 2
17 Palm Street Apple 2
city:
Cainta
Cainta
 
Hope that's helpful.
0

Featured Post

Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

What is Node.js? Node.js is a server side scripting language much like PHP or ASP but is used to implement the complete package of HTTP webserver and application framework. The difference is that Node.js’s execution engine is asynchronous and event…
I found this questions asking how to do this in many different forums, so I will describe here how to implement a solution using PHP and AJAX. The logical flow for the problem should be: Write an event handler for the first drop down box to get …
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will the learn the benefit of plain text editors and code an HTML5 based template for use in further tutorials.

772 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question