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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T20:41:35+00:00 2026-05-26T20:41:35+00:00

I am using the SAX parser in java. I am not sure: 1) What

  • 0

I am using the SAX parser in java. I am not sure:

1) What classes I need for this kind of situation? I am guessing I want to have
Classes for (please let me know if my thoughts are completely wrong):
-FosterHome (Contains an Arraylist of Family and Child)
-Family (Contains ArrayList for Child and a String fro parent)
-Child (contains ArrayList for ChildID)

2) How to handle this situation in the startElement and endElement method

What complicates is due to the ChildID appearing in both the ChildList and the RemainingChildList. Appreciate anyone who can help me out.

<FosterHome>
<Orphanage>Happy Days Daycare</Orphanage>
<Location>Apple Street</Location>
<Families>
    <Family>
        <Parent>Adams</ParentID>
        <ChildList>
            <ChildID>Child1</ChildID>
            <ChildID>Child2</ChildID>
        </ChildList>
    </Family>
    <Family>
        <Parent>Adams</ParentID>
        <ChildList>
            <ChildID>Child3</ChildID>
            <ChildID>Child4</ChildID>
        </ChildList>
    </Family>
</Families>
<RemainingChildList>
<ChildID>Child5</ChildID>
<ChildID>Child6</ChildID>
</RemainingChildList>
</FosterHome>

  • 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-26T20:41:36+00:00Added an answer on May 26, 2026 at 8:41 pm

    To handle ChildID elements from different parent elements, you can save the current state when enter startElement() handler method for ChildList and RemainingChildList respectively. Then, when you enter endElement() handler method for a ChildID, you process the current ChildID depending on the saved state (for example populate appropriate fields in your FosterHome class).

    For example:

    import org.xml.sax.Attributes;
    import org.xml.sax.InputSource;
    import org.xml.sax.SAXException;
    import org.xml.sax.XMLReader;
    import org.xml.sax.helpers.DefaultHandler;
    
    import javax.xml.parsers.ParserConfigurationException;
    import javax.xml.parsers.SAXParserFactory;
    
    import java.io.CharArrayReader;
    import java.io.CharArrayWriter;
    import java.io.IOException;
    
    public class XmlParser {
    
    static void parse(String xml, Handler handler) throws SAXException, ParserConfigurationException, IOException
    {
        SAXParserFactory saxParserFactory = SAXParserFactory.newInstance();
        XMLReader reader = saxParserFactory.newSAXParser().getXMLReader();
        reader.setContentHandler(handler);
        reader.parse(new InputSource(new CharArrayReader(xml.toCharArray())));
    }
    
    static class Handler extends DefaultHandler {
    
        CharArrayWriter contents = new CharArrayWriter();
    
        static enum STATE { Family, Remaining }
        STATE state;
    
        @Override
        public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException
        {
            if (qName.equals("ChildList"))
                state = STATE.Family;
            else if (qName.equals("RemainingChildList"))
                state = STATE.Remaining;
            else if (qName.equals("ChildID"))
                contents.reset();
        }
    
        @Override
        public void endElement(String uri, String localName, String qName) throws SAXException
        {
            if (qName.equals("ChildID"))
                System.out.println(contents.toString() + " [" + state + "]");
        }
    
        @Override
        public void characters(char[] ch, int start, int length) throws SAXException
        {
            contents.write(ch, start, length);
        }
    }
    
    public static void main(String[] args) throws Exception
    {
        String xml = "<FosterHome>\n" +
            "<Orphanage>Happy Days Daycare</Orphanage>\n" +
            "<Location>Apple Street</Location>\n" +
            "<Families>\n" +
            "    <Family>\n" +
            "        <ParentID>Adams</ParentID>\n" +
            "        <ChildList>\n" +
            "            <ChildID>Child1</ChildID>\n" +
            "            <ChildID>Child2</ChildID>\n" +
            "        </ChildList>\n" +
            "    </Family>\n" +
            "    <Family>\n" +
            "        <ParentID>Adams</ParentID>\n" +
            "        <ChildList>\n" +
            "            <ChildID>Child3</ChildID>\n" +
            "            <ChildID>Child4</ChildID>\n" +
            "        </ChildList>\n" +
            "    </Family>\n" +
            "</Families>\n" +
            "<RemainingChildList>\n" +
            "<ChildID>Child5</ChildID>\n" +
            "<ChildID>Child6</ChildID>\n" +
            "</RemainingChildList>\n" +
            "</FosterHome>";
    
        parse(xml, new Handler());
    }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have student.xml file and am parsing this file using SAX Parser and now
I have to parse a XML structure in JAVA using the SAX parser. The
I need to parse document using SAX parser in java. I was able to
I have been using a SAX parser for a while now to get data
I have a problem with using the SAX parser to parse a XML file.
I have a java SAX parser which I would like to call multiple times
When i try to read the xml from java using SAX parser, it is
I want to extract attributes from an xml file using Sax Parser I am
I am using the SAX parser in java to read some XML. The XML
I've encountered (what I think is) a strange behavior when using the sax parser,

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.