DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 4 of 4
  1. #1
    Join Date
    Apr 2004
    Cape Town, SA

    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:Message>GET_CUSTOMER : SUCCESS</ns:Message>
    <ns:IsLive xsi:type="xsd:boolean">true</ns:IsLive>

    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
    _/\/ \/\_

  2. #2
    Join Date
    Nov 2004

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

    I am definitely no xml expert but this hack produced the name value
    public String parseXML(Document doc, String tagname) {
        StringBuffer sb = new StringBuffer();
        NodeList nodes = doc.getElementsByTagName(tagname);
        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(",");
        return sb.toString();
    eschew obfuscation

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

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

    Keep well
    _/\/ \/\_

  4. #4
    Join Date
    Apr 2008

    Parsing xml with attributes using java

    Hi ,

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

    <feature id="sessionbean" label="Session Bean Demo">
    <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>

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


Similar Threads

  1. learning c# very confusing.
    By Mike Tsakiris in forum .NET
    Replies: 11
    Last Post: 10-04-2002, 05:32 PM
  2. Replies: 1
    Last Post: 05-02-2002, 07:57 AM
  3. Try XML Junction
    By Tim in forum xml.announcements
    Replies: 0
    Last Post: 10-11-2001, 04:00 PM
  4. Data Junction Announces XML Junction 7.51
    By Tim Frost in forum xml.announcements
    Replies: 0
    Last Post: 04-02-2001, 10:53 AM
  5. Replies: 1
    Last Post: 03-20-2001, 02: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
Latest Articles
Questions? Contact us.
Web Development
Latest Tips
Open Source

   Development Centers

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

We have made updates to our Privacy Policy to reflect the implementation of the General Data Protection Regulation.