Solved

Parsing XML in ASP 3.0

Posted on 2008-10-20
4
402 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
Comment Utility
0
 

Author Comment

by:jgsimbulan
Comment Utility
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
Comment Utility
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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

This article covers the basics of the Sass, which is a CSS extension language. You will learn about variables, mixins, and nesting.
This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

743 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now