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
Solved

Parsing XML in ASP 3.0

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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Suggested Solutions

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.
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).

856 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