Solved

Parsing XML in ASP 3.0

Posted on 2008-10-20
4
407 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
links not working 2 25
Triple line in an alert message 4 19
Put POST values into cookies. 14 28
How do i use the await event in php echo 5 13
This demonstration started out as a follow up to some recently posted questions on the subject of logging in: http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/Q_28634665.html and http://www.experts-exchange.com/Programming/…
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.
The viewer will learn how to count occurrences of each item in an array.
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.

685 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