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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T02:09:41+00:00 2026-06-08T02:09:41+00:00

how to parse xml using sax parser? i struggle to parse the author section

  • 0

how to parse xml using sax parser? i struggle to parse the author section part into array. i follow example from here http://ganeshtiwaridotcomdotnp.blogspot.com/2011/08/xml-parsing-using-saxparser-with.html

XML

<catalog>
    <book id="001" lang="ENG">
        <isbn>23-34-42-3</isbn>
        <regDate>1990-05-24</regDate>
        <title>Operating Systems</title>
        <publisher country="USA">Pearson</publisher>
        <price>400</price>
        <authors>
            <author>
                <name>Ganesh Tiwari</name>
                <age>1</age>
            </author>
        </authors>
    </book>
    <book id="002">
        <isbn>24-300-042-3</isbn>
        <regDate>1995-05-12</regDate>
        <title>Distributed Systems</title>
        <publisher country="Nepal">Ekata</publisher>
        <price>500</price>
        <authors>
            <author>
                <name>Mahesh Poudel</name>
                <age>2</age>
            </author>
                <author>
                <name>Bikram Adhikari</name>
                <age>3</age>
            </author>
            <author>
                <name>Ramesh Poudel</name>
                <age>4</age>
            </author>
        </authors>
        </book>
</catalog>

start

public BookSaxParser() {
    bookL = new ArrayList<Book>();
    authorL = new ArrayList<Author>();
}


public List<Book> printDatas() {

    return bookL;
}

@Override 
public void startElement(String s, String s1, String elementName, Attributes attributes) throws SAXException {
    // if current element is book , create new book
    // clear tmpValue on start of element

    if (elementName.equalsIgnoreCase("book")) {
        bookTmp = new Book();
        bookTmp.setId(attributes.getValue("id"));
        bookTmp.setLang(attributes.getValue("lang"));

    }

    if (elementName.equalsIgnoreCase("author")) {
        authorTmp = new Author();

    }
    // if current element is publisher
    if (elementName.equalsIgnoreCase("publisher")) {
        bookTmp.setPublisher(attributes.getValue("country"));
    }

}
@Override
public void endElement(String s, String s1, String element) throws SAXException {
    // if end of book element add to list
    if (element.equals("book")) {
        bookL.add(bookTmp);
    }


    if (element.equals("authors")) {
        bookTmp.setAuthors(authorL);

    }

    if (element.equals("author")) {
        authorL.add(authorTmp);

    }

    if(element.equalsIgnoreCase("name")){
        authorTmp.setName(tmpValue);

    }

    if(element.equalsIgnoreCase("age")){
        authorTmp.setAge(Integer.parseInt(tmpValue));
    }




    if (element.equalsIgnoreCase("isbn")) {
        bookTmp.setIsbn(tmpValue);
    }
    if (element.equalsIgnoreCase("title")) {
        bookTmp.setTitle(tmpValue);
    }

    if(element.equalsIgnoreCase("price")){
        bookTmp.setPrice(Integer.parseInt(tmpValue));
    }

    if(element.equalsIgnoreCase("regDate")){
        try {
            bookTmp.setRegDate(sdf.parse(tmpValue));
        } catch (ParseException e) {
            //System.out.println("date parsing error");
        }
    }
}
@Override
public void characters(char[] ac, int i, int j) throws SAXException {
    tmpValue = new String(ac, i, j);
}

public void endDocument() throws SAXException {
    // you can do something here for example send
    // the Channel object somewhere or whatever.
}

`

  • 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-08T02:09:42+00:00Added an answer on June 8, 2026 at 2:09 am

    Your code is adding authors to the authorL list as you parse them, and then assigning that same list to each of the books.

    To fix this you need to create a new list of authors each time you encounter the Authors element

    Something like:

    public void startElement(String s, String s1, String elementName, Attributes attributes) throws SAXException {
        // Your existing code here
    
        if (elementName.equalsIgnoreCase("authors")) {
            authorL = new ArrayList<Author>();
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am using SAX Parser to parse XML data from a http url. I
I trying to parse xml from an inputstream using the sax parser. The inputstream
I have a problem with using the SAX parser to parse a XML file.
I am using SAX Parser to parse the XML file over the network. While
i try to parse an rdf file using android.sax and android.utils.Xml methods. My parser
I am trying to parse XML in Perl using XML::SAX parser . My query
I have to parse a XML structure in JAVA using the SAX parser. The
I am using SAX to parse an XML file I'm pulling from the web.
I want to parse an XML file using SAX parser in java with this
When i try to read the xml from java using SAX parser, it is

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.