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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T13:55:22+00:00 2026-05-31T13:55:22+00:00

I am parsing a big XML document using a SAX parser, but after seeing

  • 0

I am parsing a big XML document using a SAX parser, but after seeing an example of SAX parsing on Google, I am unable to parse this XML:

<?xml version="1.0" encoding="UTF-8"?>
<school title="The Clifton School" icon="" browserBackButtonTitle = "Clifton App" navBarColor = "#7eb432">
    <screen id = "1" backgroundColor = "" backgroundImg = "" templateId = "12" hasNavigationBar = "0" hasTabBar = "1" >
        <navigation-bar title = "" color = "#7eb432" backButtonTitle = "Back">
            <!--<navigation-item type = "1" action = "" />-->
        </navigation-bar>
        <tab-bar numberOfTabs = "4" >
            <tab-bar-item title = "Home" image = "tab_home.png" linkedScreen = "101" />
            <tab-bar-item title = "Calendar" image = "tab_calendar.png" linkedScreen = "102" />
            <tab-bar-item title = "Menu" image = "tab_menu.png" linkedScreen = "604" />
            <tab-bar-item title = "Directions" image = "tab_directions.png" linkedScreen = "401" />
            <tab-bar-item title = "Contact" image = "tab_contact.png" linkedScreen = "206" />
        </tab-bar>
    </screen>

This is not whole XML document. For parsing that, I made 5 property classes:

1) School

public class School 
{
    public String title;
    public String icon="";
    public String browserBackButtonTitle;
    public String navBarColor;
    public ArrayList<Screen> screenlist = new ArrayList<Screen>();
}

2) Screen

public class Screen 
{
    public String Id;
    public String backgroundColor;
    public String backgroundImg ; 
    public String templateId;
    public String hasNavigationBar;
    public String hasTabBar;
    public ArrayList<NavigationBar> objlistofNB = new ArrayList<NavigationBar>();
}

3) NavigationBar

public class NavigationBar 
{
    public String title;
    public String  color;
    public String backButtonTitle;
}

4) ScreenTabBar

public class ScreenTabBar
{
    private int numberOfTabs;
    private ArrayList<TabBarItem> objlistofTabBarItem = new ArrayList<TabBarItem>();
}

5) TabBarItem

public class TabBarItem
{
    public String Title;
    public String image;
    public String linkedScreen;
}

I overrode the startElement method in which I can’t do how apply exact codition-

@Override
public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException
{
    super.startElement(uri, localName, qName, attributes);
    currentElement = true;

    if (localName.equals("school"))
    {
        /** Start */
        objschool = new School();
        schoolmap = new HashMap<String, String>();
        schoolmap.put("school_name", attributes.getValue("title"));
        schoolmap.put("school_icon", attributes.getValue("icon"));
        schoolmap.put("school_browserBackButtonTitle", attributes.getValue("browserBackButtonTitle"));
        schoolmap.put("school_navBarColor", attributes.getValue("navBarColor"));
        objschool = (School)parseProperty(objschool,schoolmap);
    }
    else if (localName.equals("screen"))
    {
        /** Get attribute value */
        objscreen = new Screen();
        screenmap.put("screen_id", attributes.getValue("id"));
        screenmap.put("screen_backgroundColor", attributes.getValue("backgroundColor"));
        screenmap.put("screen_backgroundImg",attributes.getValue("backgroundImg"));
        screenmap.put("screen_templateId",attributes.getValue("templateId"));
        screenmap.put("screen_hasNavigationBar",attributes.getValue("hasNavigationBar"));
        screenmap.put("screen_hasTabBar",attributes.getValue("hasTabBar"));
        objscreen =(Screen)parseProperty(objscreen,screenmap);
    }
    else if (localName.equals("navigation-bar"))
    {
        objnavBar = new NavigationBar();
        navimap.put("naviTitle",attributes.getValue("title"));
        navimap.put("navicolor",attributes.getValue("color"));
        navimap.put("navibackButtonTitle",attributes.getValue("backButtonTitle"));
        objnavBar =(NavigationBar)parseProperty(objnavBar,navimap);
    }
    else if (localName.equals("tab-bar"))
    {
        objtabBar = new ScreenTabBar();
        tabbarmap.put("numberOfTabs",attributes.getValue("numberOfTabs"));
        objtabBar = (ScreenTabBar)parseProperty(objtabBar, tabbarmap);
    }
    else if (localName.equals("tab-bar-item"))
    {
        objtabBaritem = new TabBarItem();
        tabbarmap.put("Title",attributes.getValue("title"));
        tabbarmap.put("Image",attributes.getValue("image"));
        tabbarmap.put("LinkedScreen",attributes.getValue("linkedScreen"));
    }
}

The parseProperty method is:

private Object parseProperty(Object t, Map<String,String> list) 
{
    Class c=null;
    String className[]=t.toString().split("@");
    try {
        String s = t.toString();
        c = Class.forName(className[0]);
        Field[] f = c.getDeclaredFields();
        //for(int i=0; i<list.entrySet().size();i++)
        //{
        for (String key : list.keySet()) {
            System.out.println("key/value: " + key + "/"+list.get(key));
            String attrname = key;
            for(int j=0; j<f.length;j++)
            {
                String s2=f[j].getName();
                if(s2.equals(attrname))
                {
                    f[j].set(t,list.get(key));
                }
            }
        }
    } catch (ClassNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IllegalArgumentException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IllegalAccessException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    return t;
}

How I can parse the above XML by the above rules which were applied by me?

  • 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-31T13:55:23+00:00Added an answer on May 31, 2026 at 1:55 pm

    Hi Please try this code you need to use correct position of tags.Hope this code will works

    InputStream in =response.getEntity().getContent();
    
                DocumentBuilder builder = DocumentBuilderFactory.newInstance()
                        .newDocumentBuilder();
                Document doc = builder.parse(in);
                String responseCode = "";
                String extendedMessage = "";
                if (doc != null) {
                    NodeList nl = doc.getElementsByTagName("school");
                    if (nl.getLength() > 0) {
                        Node node = nl.item(positionof tab-bar);
                       Node node1= node.getChildNodes().item(position of tab-bar-item);
                    responseCode = node.getAttributes().getNamedItem("title");
                }
    

    Please find the following code i parsed almost all child nodes and attribute values within readxml function as i dont have the link of that xml i used assets folder to read that xml file.With in sop statements can find how to get values from child nodes as well as attributes

    public class XMLSaxParse extends Activity {
        /** Called when the activity is first created. */
        AssetManager assetManager;
        InputStream inputStream;
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);
           try {
            inputStream=getAssets().open("sam.xml",Context.MODE_PRIVATE);
            readXML();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    
    
    
        }
    
        public void readXML()
        {
    
    
            try {
                DocumentBuilder builder = DocumentBuilderFactory.newInstance()
                        .newDocumentBuilder();
                Document doc = builder.parse(inputStream);
                if (doc != null) {
                    NodeList nl = doc.getElementsByTagName("screen");
                    NodeList nodeList=doc.getChildNodes();
                    System.out.println("Length of NodeList...................................");
                   System.out.println("Displays id node name from screen tag..................................."+nl.item(0).getAttributes().item(0).getNodeName());
                   System.out.println("Displays id node value from screen tag..................................."+nl.item(0).getAttributes().item(0).getNodeValue());
                   System.out.println("Displays naviagtionbar node name (child node)..................................."+nl.item(0).getChildNodes().item(1).getNodeName());
                   System.out.println("Displays naviagtionbar node Value: (child node)"+nl.item(0).getChildNodes().item(1).getAttributes().item(0).getNodeValue());
                   System.out.println("Displays tabbar node name (child node)..................................."+nl.item(0).getChildNodes().item(3).getNodeName());
                   System.out.println("Displays tabbar node Value (child node)..................................."+nl.item(0).getChildNodes().item(3).getNodeValue());
                   System.out.println("Displays tabbaritem node name (child node of tab-bar)..................................."+nl.item(0).getChildNodes().item(3).getChildNodes().item(1).getNodeName());
                   System.out.println("Displays tabbaritem node Value (child node of tab-bar)..................................."+nl.item(0).getChildNodes().item(3).getChildNodes().item(1).getAttributes().item(0).getNodeValue());
                    if (nl.getLength() > 0) {
                        Node node = nl.item(0);
                       Node node1= node.getChildNodes().item(0);
    
                  /*  NamedNodeMap responseCode1 = node1.getAttributes();
                   Node ex= responseCode1.getNamedItem("title");*/
                   System.out.println("data displaying.............................."+node.getNodeValue());
                }
    
       }
            } catch (DOMException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } 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();
            }
    }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I parse a big xml document with Sax, I want to stop parsing the
I have xml file in server. I am parsing this xml using DOM(xml is
I'm parsing a big XML file using SAXParser in Android and was wondering if
I'm parsing big XML file with XPathExpression selection for some nodes existing at various
I have a big xml file and parsing it consumes a lot of memory.
I am parsing a binary file using a specification. The file comes in big-endian
I am parsing a big number of big files and after profiling my bottleneck
I want to parse an XML file using Perl . I was able to
I am parsing through a XML file that's about 12mb big. I need to
I am working on parsing a big xml and storing it into a temporary

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.