Parsing XML in Java


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 4 of 4

Thread: Parsing XML in Java

  1. #1
    Join Date
    Apr 2004
    Location
    Cape Town, SA
    Posts
    8

    Exclamation Parsing XML in Java

    Hello Experts

    I got a slight problem. I have been trying to read in XML in Java using the javax.xml.parsers.DocumentBuilder and javax.xml.parsers.SAXParser classes but to no success . I new to using XML with java.

    What I need is to parse XML and be to retrieve the values or each tag.

    Below is a sample of the XML i need to pass:

    <?xml version="1.0" encoding="utf-8" ?>
    <ns:SISubscriberProfile xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:ns="http://www.openuri.org/">
    <ns:Type>1</ns:Type>
    <ns:Message>GET_CUSTOMER : SUCCESS</ns:Message>
    <ns:ErrorCode>0</ns:ErrorCode>
    <ns:Value>
    <ns:FirstName>Shaun</ns:FirstName>
    </ns:Value>
    <ns:IsLive xsi:type="xsd:boolean">true</ns:IsLive>
    </ns:SISubscriberProfile>

    If you take the line <ns:FirstName>Shaun</ns:FirstName>. I need to be able to pass a string (tag name, example "ns:FirstName") to a method and it should return the value of that tag as a string ("Shaun"). All the values i need to read are Strings.

    I use the basic code in the attachment (rename file to *.java).

    I can read the tagname but i can't get the value . What am i doing wrong? If possible could someone help me out by showing me the right code to read the value between the XML tag.

    Thank you.
    Attached Files Attached Files
    ___
    _/\/ \/\_
    (MC3)RaVeN

  2. #2
    Join Date
    Nov 2004
    Location
    Norway
    Posts
    1,560

    XML is the answer, but what is the problem...?

    I am definitely no xml expert but this hack produced the name value
    Code:
    public String parseXML(Document doc, String tagname) {
        StringBuffer sb = new StringBuffer();
        NodeList nodes = doc.getElementsByTagName(tagname);
        sb.append(tagname).append("=");
    
        for (int i = 0; i < nodes.getLength(); i++) {
    
          Node node = nodes.item(i);
    
          NodeList nList = node.getChildNodes();
    
          for (int j = 0; j < nList.getLength(); j++) {
            Node n = nList.item(j);
    
            System.out.println("getNodeValue: " + n.getNodeValue());
            if (j > 0) sb.append(",");
            sb.append(n.getNodeValue());
          }
        }
    
        return sb.toString();
      }
    eschew obfuscation

  3. #3
    Join Date
    Apr 2004
    Location
    Cape Town, SA
    Posts
    8
    Hi Sjalle.

    Thanks .
    It seems to work great. Your a genius.

    Keep well
    ___
    _/\/ \/\_
    (MC3)RaVeN

  4. #4
    Join Date
    Apr 2008
    Posts
    2

    Parsing xml with attributes using java

    Hi ,

    I have a problem of parsing an xml with attributes.This is the xml i have.

    <featureset>
    <feature id="sessionbean" label="Session Bean Demo">
    <executorClass></executorClass>
    <Params>
    <param id="drivername" label="Driver Name" value="org.jabo"></param>
    <param id="drivername" label="Driver Name" value="org.jabo"></param>
    <param id="drivername" label="Driver Name" value="org.jabo"></param>
    </Params>
    </feature>
    </featureset>

    Can you please help me to parse this and to store the values of id ,label of feature and param tags.Please help me.

    Thanks,
    laxmi

Similar Threads

  1. learning c# very confusing.
    By Mike Tsakiris in forum .NET
    Replies: 11
    Last Post: 10-04-2002, 06:32 PM
  2. Replies: 1
    Last Post: 05-02-2002, 08:57 AM
  3. Try XML Junction
    By Tim in forum xml.announcements
    Replies: 0
    Last Post: 10-11-2001, 05:00 PM
  4. Data Junction Announces XML Junction 7.51
    By Tim Frost in forum xml.announcements
    Replies: 0
    Last Post: 04-02-2001, 11:53 AM
  5. Replies: 1
    Last Post: 03-20-2001, 03:31 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center