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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T18:22:00+00:00 2026-06-02T18:22:00+00:00

I’m trying to parse an xml file but seem not to understand how it

  • 0

I’m trying to parse an xml file but seem not to understand how it works. I have been debugging for hours but cant seem to get the correct value. I have managed to get the code working for the tracklist tag, but not for the playbacklist tag and his children tags.

I’m would like to have the values of a playback device, in the future more will be added.

this is the xml source:

<?xml version="1.0" encoding="ISO-8859-1"?>
<root>
<tracklist>
    <track>track001.mp3</track>
    <track>track002.mp3</track>
    <track>track003.mp3</track>
    <track>track004.mp3</track>
    <track>track005.mp3</track>
    <track>track006.mp3</track>
    <track>track007.mp3</track>
    <track>track008.mp3</track>
    <track>track009.mp3</track>
    <track>track010.mp3</track>
</tracklist>
<playbacklist>
    <playback>
        <name>Speaker1</name>
        <ip>192.168.1.103</ip>
        <room>Kitchen</room>
        <options>0</options>
        <state>NotPlaying</state>
    </playback>
</playbacklist>
</root>

this is the java code (snippets of the code): this code is working for me

DocumentBuilder db = factory.newDocumentBuilder();
Document doc = db.parse(inStream);
NodeList nodeList = doc.getElementsByTagName("root");

for (int index = 0; index < nodeList.getLength(); index++) {
Node node = nodeList.item(index);
if (node.getNodeType() == Node.ELEMENT_NODE) {
    Element element = (Element) node;
    NodeList nameNode = element.getChildNodes();
    for (int iIndex = 0; iIndex < nameNode.getLength(); iIndex++) {
        if (nameNode.item(iIndex).getNodeType() == Node.ELEMENT_NODE) {
                Element nameElement = (Element) nameNode.item(iIndex);
                    if(nameElement.getNodeName().equals("tracklist")){
                NodeList trackNodes = nameElement.getChildNodes();
            for(int i=0;i<trackNodes.getLength();i++){
                if (trackNodes.item(i).getNodeType() == Node.ELEMENT_NODE) {
                    Element trackElement = (Element) trackNodes.item(i);
                    playlist.add(trackElement.getFirstChild().getNodeValue());
                    }
                }
            } 

this code isn’t working for me:

        if(nameElement.getNodeName().equals("playbacklist")){
            NodeList devicesNodes = nameElement.getChildNodes();
            for(int j=0;j<=devicesNodes.getLength();j++){
                Node nodeDevice = devicesNodes.item(j);
                NodeList childNodes = nodeDevice.getChildNodes();
  • 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-02T18:22:03+00:00Added an answer on June 2, 2026 at 6:22 pm

    Here is your SaxParser generated by my Sax Class Generator

    package sherif.java.sax;
    
    import org.xml.sax.SAXException;
    import org.xml.sax.helpers.DefaultHandler;
    
    public class YourHandler extends DefaultHandler
    {
        //TAGS      /\Sherif/\
        private boolean root = false;
        private boolean playbacklist = false;
        private boolean playback = false;
        private boolean name = false;
        private boolean ip = false;
        private boolean room = false;
        private boolean options = false;
        private boolean state = false;
        private boolean tracklist = false;
        private boolean track = false;
    
        public YourHandler()
        {
            //TODO      /\Sherif/\
        }
    
        @Override
        public void startDocument() throws SAXException
        {
            super.startDocument();
            //TODO      /\Sherif/\
        }
    
        @Override
        public void endDocument() throws SAXException
        {
            super.endDocument();
            //TODO      /\Sherif/\
        }
    
        @Override
        public void characters(char sherifCh[], int sherifSt, int sherifle)
        {
            String value = (new String(sherifCh)).substring(sherifSt, sherifSt + sherifle);
            if(root)
            {
                if(playbacklist)
                {
                    if(playback)
                    {
                        if(name)
                        {
                            //TODO      /\Sherif/\
    
                        }
                        else if(ip)
                        {
                            //TODO      /\Sherif/\
    
                        }
                        else if(room)
                        {
                            //TODO      /\Sherif/\
    
                        }
                        else if(options)
                        {
                            //TODO      /\Sherif/\
    
                        }
                        else if(state)
                        {
                            //TODO      /\Sherif/\
    
                        }
                    }
                }
                else if(tracklist)
                {
                    if(track)
                    {
                        //TODO      /\Sherif/\
    
                    }
                }
            }
        }
    
        @Override
        public void startElement(String sherifUr, String sherifNa, String sherifQn, org.xml.sax.Attributes sherifAt) throws SAXException
        {
            super.startElement(sherifUr, sherifNa, sherifQn, sherifAt);
            if(sherifNa.equals("root"))
            {
                this.root = true;
            }
            else if(sherifNa.equals("playbacklist"))
            {
                this.playbacklist = true;
            }
            else if(sherifNa.equals("playback"))
            {
                this.playback = true;
            }
            else if(sherifNa.equals("name"))
            {
                this.name = true;
            }
            else if(sherifNa.equals("ip"))
            {
                this.ip = true;
            }
            else if(sherifNa.equals("room"))
            {
                this.room = true;
            }
            else if(sherifNa.equals("options"))
            {
                this.options = true;
            }
            else if(sherifNa.equals("state"))
            {
                this.state = true;
            }
            else if(sherifNa.equals("tracklist"))
            {
                this.tracklist = true;
            }
            else if(sherifNa.equals("track"))
            {
                this.track = true;
            }
        }
    
        @Override
        public void endElement(String sherifUr, String sherifNa, String sherifQn) throws SAXException
        {
            super.endElement(sherifUr, sherifNa, sherifQn);
            if(sherifNa.equals("root"))
            {
                this.root = false;
            }
            else if(sherifNa.equals("playbacklist"))
            {
                this.playbacklist = false;
            }
            else if(sherifNa.equals("playback"))
            {
                this.playback = false;
            }
            else if(sherifNa.equals("name"))
            {
                this.name = false;
            }
            else if(sherifNa.equals("ip"))
            {
                this.ip = false;
            }
            else if(sherifNa.equals("room"))
            {
                this.room = false;
            }
            else if(sherifNa.equals("options"))
            {
                this.options = false;
            }
            else if(sherifNa.equals("state"))
            {
                this.state = false;
            }
            else if(sherifNa.equals("tracklist"))
            {
                this.tracklist = false;
            }
            else if(sherifNa.equals("track"))
            {
                this.track = false;
            }
        }
    }
    

    You can use it:

    String yourXmlString;  
    SAXParserFactory spf = SAXParserFactory.newInstance();  
    SAXParser sp = spf.newSAXParser();  
    XMLReader xr = sp.getXMLReader();  
    
    /* Create a new instance of the class generated */  
    YourHandler handler = new YourHandler ();  
    xr.setContentHandler(handler);  
    
    InputSource inputSource = new InputSource();  
    inputSource.setEncoding("UTF-8");  
    inputSource.setCharacterStream(new StringReader(response));  
    
    /* Start Parsing */  
    xr.parse(inputSource);  
    /* Parsing Done. */  
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a French site that I want to parse, but am running into
I am trying to understand how to use SyndicationItem to display feed which is
I have just tried to save a simple *.rtf file with some websites and
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I am trying to render a haml file in a javascript response like so:
In my XML file chapters tag has more chapter tag.i need to display chapters
We are using XSLT to translate a RIXML file to XML. Our RIXML contains
i want to parse a xhtml file and display in UITableView. what is the
I have a reasonable size flat file database of text documents mostly saved in
I'm parsing an XML file, the creators of it stuck in a bunch social

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.