?
Solved

Parsing XML in ASP 3.0

Posted on 2008-10-20
4
Medium Priority
?
410 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
4 Comments
 
LVL 16

Accepted Solution

by:
Gyanendra Singh earned 1000 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 1000 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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

SASS allows you to treat your CSS code in a more OOP way. Let's have a look on how you can structure your code in order for it to be easily maintained and reused.
This article discusses how to implement server side field validation and display customized error messages to the client.
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
HTML5 has deprecated a few of the older ways of showing media as well as offering up a new way to create games and animations. Audio, video, and canvas are just a few of the adjustments made between XHTML and HTML5. As we learned in our last micr…
Suggested Courses

801 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