Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4585862
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T21:33:23+00:00 2026-05-21T21:33:23+00:00

I have these XML on a URL <?xml version=1.0 encoding=ISO-8859-1 ?> <Phonebook> <PhonebookEntry> <firstname>John</firstname>

  • 0

I have these XML on a URL

<?xml version="1.0" encoding="ISO-8859-1" ?> 
<Phonebook>
    <PhonebookEntry>
        <firstname>John</firstname> 
        <lastname>Connor</lastname> 
        <Address>5,Downing Street</Address> 
        <Phone loc="home">9875674567</Phone> 
        <Phone loc="work">9875674567</Phone> 
        <Phone loc="mobile">78654562341</Phone> 
    </PhonebookEntry>
    <PhonebookEntry>
        <firstname>John</firstname> 
        <lastname>Smith</lastname> 
        <Address>6,Downing Street</Address> 
        <Phone loc="home">678-56-home</Phone> 
        <Phone loc="work">678-59-work</Phone> 
        <Phone loc="mobile">678-85-mobile</Phone> 
    </PhonebookEntry>
</Phonebook>

I was able to extract the values on firstname, lastname and Address, but when it comes to Phone tags, my code returns null values. Here are my code:

ParsingXML.java

package com.example.parsingxml;

import java.net.Proxy;
import java.net.Socket;
import java.net.SocketAddress;
import java.net.URL;
import java.net.URLConnection;

import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;

import org.xml.sax.InputSource;
import org.xml.sax.XMLReader;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;
public class ParsingXML extends Activity {



    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle icicle) {
         super.onCreate(icicle);
         //System.setProperty("http.proxyHost"," 129.188.69.100 "); 
         //System.setProperty("http.proxyPort","1080");
         //System.setProperty("http.nonProxyHosts","10.228.97.76");

         /* Create a new TextView to display the parsingresult later. */
         TextView tv = new TextView(this);
         try {
              /* Create a URL we want to load some xml-data from. */
              URL url = new URL("http://somedomain.com/jm/sampleXML.xml");
                           URLConnection ucon = url.openConnection();
              /* Get a SAXParser from the SAXPArserFactory. */
              SAXParserFactory spf = SAXParserFactory.newInstance();
              SAXParser sp = spf.newSAXParser();

              /* Get the XMLReader of the SAXParser we created. */
              XMLReader xr = sp.getXMLReader();
              /* Create a new ContentHandler and apply it to the XML-Reader*/
              ExampleHandler myExampleHandler = new ExampleHandler();
              xr.setContentHandler(myExampleHandler);

              /* Parse the xml-data from our URL. */
              xr.parse(new InputSource(url.openStream()));
              /* Parsing has finished. */

              /* Our ExampleHandler now provides the parsed data to us. */
              ParsedExampleDataSet parsedExampleDataSet =
                                            myExampleHandler.getParsedData();

              /* Set the result to be displayed in our GUI. */
              tv.setText(parsedExampleDataSet.toString());

         } catch (Exception e) {
              /* Display any Error to the GUI. */
              tv.setText("Error: " + e.getMessage());

         }
         /* Display the TextView. */
         this.setContentView(tv);
    }
}

ExampleHandler.java

package com.example.parsingxml;

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;


public class ExampleHandler extends DefaultHandler{

     // ===========================================================
     // Fields
     // ===========================================================

     private boolean in_outertag = false;
     private boolean in_innertag = false;
     private boolean in_firstname = false;
     private boolean in_lastname= false;
     private boolean in_Address=false;
     private boolean in_Phone=false;
     private boolean in_homePhone=false;
     private boolean in_workPhone=false;
     private boolean in_mobilePhone=false;


     private ParsedExampleDataSet myParsedExampleDataSet = new ParsedExampleDataSet();

     // ===========================================================
     // Getter & Setter
     // ===========================================================

     public ParsedExampleDataSet getParsedData() {
          return this.myParsedExampleDataSet;
     }

     // ===========================================================
     // Methods
     // ===========================================================
     @Override
     public void startDocument() throws SAXException {
          this.myParsedExampleDataSet = new ParsedExampleDataSet();
     }

     @Override
     public void endDocument() throws SAXException {
          // Nothing to do
     }

     /** Gets be called on opening tags like:
      * <tag>
      * Can provide attribute(s), when xml was like:
      * <tag attribute="attributeValue">*/
     @Override
     public void startElement(String namespaceURI, String localName,
               String qName, Attributes atts) throws SAXException {
          if (localName.equals("PhoneBook")) {
               this.in_outertag = true;
          }else if (localName.equals("PhonebookEntry")) {
               this.in_innertag = true;
          }else if (localName.equals("firstname")) {
               this.in_firstname = true;
          }else if (localName.equals("lastname"))  {
              this.in_lastname= true;
          }else if(localName.equals("Address"))  {
              this.in_Address= true;
          }else if (localName.equals("Phone")){
              this.in_Phone=true;
              String phoneattr=atts.getValue("loc");
              if(phoneattr.equals("home")){
                this.in_homePhone=true;
              }else if(phoneattr.equals("work")){
                this.in_workPhone=true;
              }else if(phoneattr.equals("mobile")){
                this.in_mobilePhone=true;
             }
        }  


          }

              //else if (localName.equals("tagwithnumber")) {
         // }
               // Extract an Attribute
              // String attrValue = atts.getValue("thenumber");
              // int i = Integer.parseInt(attrValue);
              // myParsedExampleDataSet.setExtractedInt(i);
        //  }


     /** Gets be called on closing tags like:
      * </tag> */
     @Override
     public void endElement(String namespaceURI, String localName, String qName)
               throws SAXException {
          if (localName.equals("Phonebook")) {
               this.in_outertag = false;
          }else if (localName.equals("PhonebookEntry")) {
               this.in_innertag = false;
          }else if (localName.equals("firstname")) {
               this.in_firstname = false;
          }else if (localName.equals("lastname"))  {
              this.in_lastname= false;
          }else if(localName.equals("Address"))  {
              this.in_Address= false;
          }else if(localName.equals("Phone"))   {
              this.in_Phone=false;
          }
     }

     /** Gets be called on the following structure:
      * <tag>characters</tag> */
     @Override
    public void characters(char ch[], int start, int length) {
          if(this.in_firstname){
          myParsedExampleDataSet.setfirstname(new String(ch, start, length));
          }
          if(this.in_lastname){
          myParsedExampleDataSet.setlastname(new String(ch, start, length));
          }
          if(this.in_Address){
              myParsedExampleDataSet.setAddress(new String(ch, start, length));
          }
          if(this.in_homePhone){
              myParsedExampleDataSet.sethomePhone(new String(ch, start, length));
          }
          if(this.in_workPhone){
              myParsedExampleDataSet.setworkPhone(new String(ch, start, length));
          }
          if(this.in_mobilePhone){
              myParsedExampleDataSet.setmobilePhone(new String(ch, start, length));
          }
    }
}

ParsedExampleDataSet.java

package com.example.parsingxml;

public class ParsedExampleDataSet {
    private String firstname = null;
    private String lastname=null;
    private String Address=null;
    private String Phone=null;
    private String homephone=null;
    private String workphone=null;
    private String mobilephone=null;


    //Firstname
    public String getfirstname() {
         return firstname;
    }
    public void setfirstname(String firstname) {
         this.firstname = firstname;
    }

    //Lastname
    public String getlastname(){
        return lastname;
    }
    public void setlastname(String lastname){
        this.lastname=lastname;
    }

    //Address
    public String getAddress(){
        return Address;
    }
    public void setAddress(String Address){
        this.Address=Address;
    }

    //Phone
    public String getPhone(){
        return Phone;
    }
    public void sethomePhone(String homePhone){
        this.homephone=homePhone;
    }
    public void setworkPhone(String homePhone){
        this.homephone=homePhone;
    }
    public void setmobilePhone(String homePhone){
        this.homephone=homePhone;
    }


    public String toString(){
         return "firstname: " + this.firstname + "\n" + "lastname=: " + this.lastname + "\n" + "Address: " + this.Address+ "\n"  + "homephone: " + this.homephone + "\n" + "workphone: " + this.workphone + "\n" + "mobilephone: " + this.mobilephone;

    }
}

Another thing how can I retrieve both values in PhonebookEntry tags?

I’m kinda new to java and android dev, many thanks in advance! 🙂

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-21T21:33:23+00:00Added an answer on May 21, 2026 at 9:33 pm

    This code makes the incorrect assumption that the characters method will be called only once between the startElement and endElement calls for a tag.

    Instead of having the logic for setting the values in the characters method, you need initialize a buffer in the startElement method, collect characters into the buffer in the characters method, and then make the assignments and clear the buffer in the endElement method.

    EDIT:

    Actually, there were a few other issues in your code…

    You had setMobilePhone and setWorkPhone in your model class setting the homePhone field… An inevitable risk when you copy-modify code.

    And your handler was only creating one result, and would have overwritten and only returned the last one in the file.

    I’ve played with it, changed some of the methods more to Java standard naming, and changed some of the xml to CamelCase as well.

    Revised XML:

    <?xml version="1.0" encoding="ISO-8859-1" ?>
    <PhoneBook>
        <PhoneBookEntry>
            <FirstName>John</FirstName>
            <LastName>Connor</LastName>
            <Address>5,Downing Street</Address>
            <Phone loc="home">9875674567</Phone>
            <Phone loc="work">9875674567</Phone>
            <Phone loc="mobile">78654562341</Phone>
        </PhoneBookEntry>
        <PhoneBookEntry>
            <FirstName>John</FirstName>
            <LastName>Smith</LastName>
            <Address>6,Downing Street</Address>
            <Phone loc="home">678-56-home</Phone>
            <Phone loc="work">678-59-work</Phone>
            <Phone loc="mobile">678-85-mobile</Phone>
        </PhoneBookEntry>
    </PhoneBook>
    

    Revised ParsedExampleDataSet:

    public class ParsedExampleDataSet {
        private String firstName = null;
        private String lastName =null;
        private String address =null;
        private String homePhone =null;
        private String workPhone =null;
        private String mobilePhone =null;
    
    
        //Firstname
        public String getFirstName() {
             return firstName;
        }
        public void setFirstName(String firstName) {
             this.firstName = firstName;
        }
    
        //Lastname
        public String getLastName(){
            return lastName;
        }
        public void setLastName(String lastName){
            this.lastName = lastName;
        }
    
        //address
        public String getAddress(){
            return address;
        }
        public void setAddress(String Address){
            this.address =Address;
        }
    
        public void setHomePhone(String homePhone){
            this.homePhone =homePhone;
        }
        public void setWorkPhone(String homePhone){
            this.workPhone =homePhone;
        }
        public void setMobilePhone(String homePhone){
            this.mobilePhone =homePhone;
        }
    
    
        public String toString(){
             return "firstName: " + this.firstName + "\n" + "lastName=: " + this.lastName + "\n" + "address: " + this.address + "\n"  + "homePhone: " + this.homePhone + "\n" + "workPhone: " + this.workPhone + "\n" + "mobilePhone: " + this.mobilePhone;
    
        }
    }
    

    And the handler that does the parsing:

    import org.xml.sax.Attributes;
    import org.xml.sax.SAXException;
    import org.xml.sax.helpers.DefaultHandler;
    
    import java.util.ArrayList;
    import java.util.List;
    
    public class ExampleHandler extends DefaultHandler {
    
        // ===========================================================
        // Fields
        // ===========================================================
    
        private boolean in_homePhone = false;
        private boolean in_workPhone = false;
        private boolean in_mobilePhone = false;
    
        private StringBuffer stringBuffer;
    
        private List<ParsedExampleDataSet> myParsedExampleDataSets;
        private ParsedExampleDataSet myParsedExampleDataSet;
    
        // ===========================================================
        // Methods
        // ===========================================================
    
        public List<ParsedExampleDataSet> getParsedData() {
            return myParsedExampleDataSets;
        }
    
        @Override
        public void startDocument() throws SAXException {
            myParsedExampleDataSets = new ArrayList<ParsedExampleDataSet>();
            stringBuffer = new StringBuffer();
        }
    
        @Override
        public void endDocument() throws SAXException {
            // Nothing to do
        }
    
        /**
         * Gets be called on opening tags like:
         * <tag>
         * Can provide attribute(s), when xml was like:
         * <tag attribute="attributeValue">
         */
        @Override
        public void startElement(String namespaceURI, String localName,
                                 String qName, Attributes atts) throws SAXException {
    
            if (qName.equals("PhoneBookEntry")) {
                myParsedExampleDataSet = new ParsedExampleDataSet();
            }
    
            if (qName.equals("Phone")) {
                String phoneLocation = atts.getValue("loc");
                if (phoneLocation.equals("home")) {
                    this.in_homePhone = true;
                } else if (phoneLocation.equals("work")) {
                    this.in_workPhone = true;
                } else if (phoneLocation.equals("mobile")) {
                    this.in_mobilePhone = true;
                }
            }
        }
    
        /**
         * Gets be called on closing tags like:
         * </tag>
         */
        @Override
        public void endElement(String namespaceURI, String localName, String qName)
                throws SAXException {
    
            String result = stringBuffer.toString();
            stringBuffer.setLength(0);
    
            if (in_homePhone) {
                myParsedExampleDataSet.setHomePhone(result);
                in_homePhone = false;
            }
            else if (in_mobilePhone) {
                myParsedExampleDataSet.setMobilePhone(result);
                in_mobilePhone = false;
            }
            else if (in_workPhone) {
                myParsedExampleDataSet.setWorkPhone(result);
                in_workPhone = false;
            }
            else if (qName.equals("FirstName")) {
                myParsedExampleDataSet.setFirstName(result);
            }
            else if (qName.equals("LastName")) {
                myParsedExampleDataSet.setLastName(result);
            }
            else if (qName.equals("Address")) {
                myParsedExampleDataSet.setAddress(result);
            }
            else if (qName.equals("PhoneBookEntry")) {
                myParsedExampleDataSets.add(myParsedExampleDataSet);
            }
        }
    
        /**
         * Gets be called on the following structure:
         * <tag>characters</tag>
         */
        @Override
        public void characters(char ch[], int start, int length) {
            stringBuffer.append(new String(ch, start, length));
        }
    
    }
    

    It puts some likely unwanted whitespace in the fields, but I suspect you can figure out how to trim that.

    Calling it, you get a list rather than a single object, but other than dealing with that, your calling code shouldn’t have to change too much. I didn’t try to work with that, as I’m not doing any of this on android.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an XML file : <?xml version=1.0 encoding=windows-1250?> <CONTACTS> <CONTACT> <FirstName>AfgZohal</FirstName> <LastName>Zohal Afg</LastName>
I have this persistence unit: <?xml version=1.0 encoding=UTF-8?> <persistence version=1.0 xmlns=http://java.sun.com/xml/ns/persistence xmlns:xsi=http://www.w3.org/2001/XMLSchema-instance xsi:schemaLocation=http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd>
I have the following xml snippet: <?xml version=1.0 encoding=UTF-8 ?> <!DOCTYPE sqlMap PUBLIC -//ibatis.apache.org//DTD
For JAAS authentication I have configured a datasource as follows: <?xml version=1.0 encoding=UTF-8?> <datasources>
I have sitemesh with spring working, this is the configuration: decorator.xml <?xml version=1.0 encoding=UTF-8?>
I have this XML file here. <?xml version=1.0 encoding=UTF-8?> <xml> <word> <threeletter>RIP</threeletter> <threeletter>PIE</threeletter> <fourletter>PIER</fourletter>
Imagine you have a data.xml file: <?xml version=1.0 encoding=UTF-8 ?> <root> <item>value1</item> <item>value2</item> <item>value3</item>
I want to parse xml files that have elements like these: <element>&amp</element> <element>&amp;</element> But
I followed these instructions here: http://logback.qos.ch/consolePlugin.html I have the correct and found logback.xml, it
I have a page that is making an XML POST-Request to an URL on

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.