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 8942503
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T11:31:58+00:00 2026-06-15T11:31:58+00:00

I would like to parse an xml file. I am trying to locate the

  • 0

I would like to parse an xml file. I am trying to locate the error, but i can not find it. Could you help me, pls?
xml:

<maintag>
    <data>
        <id>1</id>
        <name>x y</name>
        <age>16</age>
        <phone>06/30 123-4567</phone>
        <address>Veszprem Valami ut 10.</address>
    </data>
    <data>
        <id>2</id>
        <name>p q</name>
        <age>18</age>
        <phone>06/70 987-6543</phone>
        <address>Budapest Ulloi ut 21.</address>
    </data>
</maintag>

XMLParser class:

package com.example.xmlproba;

import java.util.jar.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;
import android.util.Log;

public class XMLParser extends DefaultHandler {

    boolean xmlStartLine = false;
    boolean id = false;
    boolean data = false;
    boolean maintag = false;
    boolean age = false;
    boolean name = false;
    boolean phone = false;
    boolean address = false;

    Data currentData;
    DataContainer dataContainer;

    public XMLParser(DataContainer dataContainer){
        this.dataContainer=dataContainer;
        Log.d("FUNC","XMLPARSER()");

        // Data d = new Data();
    }

    public void startElement(String uri, String localName, String qName,
            Attributes attributes) throws SAXException {

        Log.d("START","START");

        if (qName.equalsIgnoreCase("MAINTAG")) {
            Log.d("Maintagfound","mtf");

        } else if (qName.equalsIgnoreCase("DATA")) {
            //create new data object 
            currentData = new Data();
            Log.d("NEWDATA","NEWDATA");
        } else if (qName.equalsIgnoreCase("ID")) {
            id  = true;
            Log.d("id","id");
        } else if (qName.equalsIgnoreCase("NAME")) {
            name = true;
            Log.d("name","name");
        } else if (qName.equalsIgnoreCase("AGE")) {
            age = true;
        } else if (qName.equalsIgnoreCase("PHONE")) {
            phone = true;
        } else if (qName.equalsIgnoreCase("ADDRESS")) {
            address = true;
        } else {
        }

    }

    @Override
    public void endElement(String uri, String localName, String qName)
            throws SAXException {
        Log.d("END","END");
        if (qName.equalsIgnoreCase("DATA")) {
            //todo at the end of a data node
            //dataContainer.addDataToList(currentData);
            //dataContainer.dataList.add(currentData);
        }
    }

    @Override
    public void characters(char[] ch, int start, int length)
            throws SAXException {
        Log.d("CHARS","CHARS");
        if (id) {
            String s = new String(ch, start, length);
            currentData.setId(Integer.parseInt(s));
            Log.d("IDIDID",s);
            id = false;
        } else if (name) {
            String s = new String(ch, start, length);
            Log.d("NAME1",s);
            currentData.setName(s);
            Log.d("NAME2",currentData.getName());
            name = false;
        } else if (age) {
            String s = new String(ch, start, length);
            currentData.setAge(Integer.parseInt(s));

            age = false;
        } else if (phone) {
            String s = new String(ch, start, length);
            currentData.setPhone(s);
            phone = false;
        } else if (address) {
            String s = new String(ch, start, length);
            currentData.setAdress(s);
            address = false;
        } else {
        }

    }

}

And the parsing part of my activity:

private void readXML() {
    // TODO Auto-generated method stub
    Log.d("FUNC","READXML");
    try {

        SAXParserFactory spf = SAXParserFactory.newInstance();
        SAXParser sp = spf.newSAXParser();
        XMLReader xr = sp.getXMLReader();

        XMLParser xp = new XMLParser(dataContainer);
        xr.setContentHandler(xp);
        InputStream is = getResources().openRawResource(
                R.raw.data);
        xr.parse(new InputSource(is));

    } catch (ParserConfigurationException e) {
        // TODO Auto-generated catch block
        Log.d("PARSERCONFEX","PARSERCONFEX");
        e.printStackTrace();
    } catch (SAXException e) {
        // TODO Auto-generated catch block
        Log.d("SAXEX","SAXEX");
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        Log.d("IOEX","IOEX");
        e.printStackTrace();
    }

}

From the log.d() i can see that only the log from constructor, the “CHARS” and “END” logs are created.
Here is my log:

12-06 06:38:11.063: E/Trace(629): error opening trace file: No such file or directory (2)
12-06 06:38:11.623: D/FUNC(629): INIT
12-06 06:38:11.623: D/FUNC(629): READXML
12-06 06:38:11.643: D/FUNC(629): XMLPARSER()
12-06 06:38:11.664: D/CHARS(629): CHARS
12-06 06:38:11.664: D/CHARS(629): CHARS
12-06 06:38:11.664: D/CHARS(629): CHARS
12-06 06:38:11.664: D/CHARS(629): CHARS
12-06 06:38:11.664: D/CHARS(629): CHARS
12-06 06:38:11.673: D/END(629): END
12-06 06:38:11.673: D/CHARS(629): CHARS
12-06 06:38:11.673: D/CHARS(629): CHARS
12-06 06:38:11.673: D/CHARS(629): CHARS
12-06 06:38:11.673: D/END(629): END
12-06 06:38:11.673: D/CHARS(629): CHARS
12-06 06:38:11.673: D/CHARS(629): CHARS
12-06 06:38:11.683: D/CHARS(629): CHARS
12-06 06:38:11.683: D/END(629): END
12-06 06:38:11.683: D/CHARS(629): CHARS
12-06 06:38:11.683: D/CHARS(629): CHARS
12-06 06:38:11.683: D/CHARS(629): CHARS
12-06 06:38:11.683: D/END(629): END
12-06 06:38:11.683: D/CHARS(629): CHARS
12-06 06:38:11.683: D/CHARS(629): CHARS
12-06 06:38:11.693: D/CHARS(629): CHARS
12-06 06:38:11.693: D/END(629): END
12-06 06:38:11.693: D/CHARS(629): CHARS
12-06 06:38:11.693: D/CHARS(629): CHARS
12-06 06:38:11.693: D/END(629): END
12-06 06:38:11.693: D/CHARS(629): CHARS
12-06 06:38:11.693: D/CHARS(629): CHARS
12-06 06:38:11.693: D/CHARS(629): CHARS
12-06 06:38:11.693: D/CHARS(629): CHARS
12-06 06:38:11.703: D/CHARS(629): CHARS
12-06 06:38:11.703: D/END(629): END
12-06 06:38:11.703: D/CHARS(629): CHARS
12-06 06:38:11.713: D/CHARS(629): CHARS
12-06 06:38:11.713: D/CHARS(629): CHARS
12-06 06:38:11.713: D/END(629): END
12-06 06:38:11.713: D/CHARS(629): CHARS
12-06 06:38:11.713: D/CHARS(629): CHARS
12-06 06:38:11.713: D/CHARS(629): CHARS
12-06 06:38:11.713: D/END(629): END
12-06 06:38:11.713: D/CHARS(629): CHARS
12-06 06:38:11.713: D/CHARS(629): CHARS
12-06 06:38:11.723: D/CHARS(629): CHARS
12-06 06:38:11.723: D/END(629): END
12-06 06:38:11.723: D/CHARS(629): CHARS
12-06 06:38:11.723: D/CHARS(629): CHARS
12-06 06:38:11.723: D/CHARS(629): CHARS
12-06 06:38:11.723: D/END(629): END
12-06 06:38:11.733: D/CHARS(629): CHARS
12-06 06:38:11.733: D/CHARS(629): CHARS
12-06 06:38:11.733: D/END(629): END
12-06 06:38:11.733: D/CHARS(629): CHARS
12-06 06:38:11.733: D/CHARS(629): CHARS
12-06 06:38:11.733: D/END(629): END
12-06 06:38:12.063: I/Choreographer(629): Skipped 77 frames!  The application may be doing too much work on its main thread.
12-06 06:38:12.093: D/gralloc_goldfish(629): Emulator without GPU emulation detected.
  • 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-06-15T11:31:59+00:00Added an answer on June 15, 2026 at 11:31 am

    Here is an example that what you wanna do using SAX

    JAVA CLASSES

    Data

    public class Data {
    
        private Integer id;
        private String name;
        private Integer age;
        private String phone;
        private String address;
    
    
        public Data(){}
    
    
        public Integer getId() {
            return id;
        }
    
    
        public void setId(Integer id) {
            this.id = id;
        }
    
    
        public String getName() {
            return name;
        }
    
    
        public void setName(String name) {
            this.name = name;
        }
    
    
        public String getPhone() {
            return phone;
        }
    
    
        public void setPhone(String phone) {
            this.phone = phone;
        }
    
    
        public String getAddress() {
            return address;
        }
    
    
        public void setAddress(String address) {
            this.address = address;
        }
    
    
        public Integer getAge() {
            return age;
        }
    
    
        public void setAge(Integer age) {
            this.age = age;
        }
    
    
    }
    

    DatasHandler

    public class DatasHandler extends DefaultHandler {
    
        private ArrayList<Data> alDatas;
        private Data data;
        private String reading;
    
        public DatasHandler(){}
    
        @Override
        public void startElement(String uri, String localName, String qName,
                Attributes attributes) throws SAXException {
    
            if(qName.equals("maintag")){
                alDatas = new ArrayList<>();
            }
            if(qName.equals("data")){
                data = new Data();
            }
    
        }
    
        @Override
        public void endElement(String uri, String localName, String qName)
                throws SAXException {
    
            if(qName.equals("data")){
                alDatas.add(data);
                data = null;
            }
            if(qName.equals("id")){
                data.setId(Integer.parseInt(reading));
            }
            if(qName.equals("name")){
                data.setName(reading);
            }
            if(qName.equals("age")){
                data.setAge(Integer.parseInt(reading));
            }
            if(qName.equals("phone")){
                data.setPhone(reading);
            }
            if(qName.equals("address")){
                data.setAddress(reading);
            }
    
        }
    
        @Override
        public void characters(char[] ch, int start, int length)
                throws SAXException {
            reading = new String(ch, start, length);
        }
    
        public ArrayList<Data> getAlDatas() {
            return alDatas;
        }
    
    
    }
    

    XMLManager

    public final class XMLManager {
    
        public static ArrayList<Data> getAlDatas(){
            ArrayList<Data> alDatas = null;
            SAXParserFactory factory = SAXParserFactory.newInstance();
            try {
                SAXParser parser = factory.newSAXParser();
                DatasHandler dHandler = new DatasHandler();
                parser.parse(new File("D:\\Loic_Workspace\\SaxExample\\res\\test.xml"), dHandler);
                alDatas = dHandler.getAlDatas();
            } catch (ParserConfigurationException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (SAXException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
    
            return alDatas;
        }
    
    
    }
    

    Main

    public class MyMain {
    
        /**
         * @param args
         */
        public static void main(String[] args) {
    
            ArrayList<Data> alDatas = XMLManager.getAlDatas();
    
            for(Data d:alDatas){
                System.out.println(d.getName());
                System.out.println(d.getId());
                System.out.println(d.getAge());
                System.out.println(d.getPhone());
                System.out.println(d.getAddress());
                System.out.println("--------------------");
            }
    
        }
    
    }
    

    Console output

    x y
    1
    16
    06/30 123-4567
    Veszprem Valami ut 10.
    --------------------
    p q
    2
    18
    06/70 987-6543
    Budapest Ulloi ut 21.
    --------------------
    

    You seem want to use it on Android. So I think that you can do the following in XMLManager class :

    InputSource in = new InputSource(getResources().openRawResource(R.raw.data));
    parser.parse(in, dHandler);
    

    Tell me if it works,

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

Sidebar

Related Questions

I'm trying to configure IIS to parse a .xml file just like it would
I'm trying to parse an xml file but seem not to understand how it
I am trying to parse the XML file in R, so that I can
Trying to parse out this xml file to get a list of Elements but
I would like to parse an XML file and save this information into object.
Trying to parse xml to text I got something like this, INPUT FILE <Item
I'm trying to parse XML data stored in a variable, not a file. The
I'm new to jQuery and would like to parse an XML document. I'm able
I would like to read multiple XML files in an array and then parse
Would like to parse IPv4 address from exit-addresses . Format of the file: ExitNode

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.