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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T20:32:35+00:00 2026-06-10T20:32:35+00:00

I am trying to parse an xml file with SAX Parser. I need to

  • 0

I am trying to parse an xml file with SAX Parser.
I need to get attributes and it’s values of a start element

<?xml version="1.0" encoding="ISO-8859-1" ?>
<API type="Connection">
<INFO server="com.com" function="getAccount2" />
<RESULT code="0">Operation Succeeded</RESULT>
<RESPONSE numaccounts="1">
<ACCOUNT login="fa051981" skynum="111111" maxaliases="1" creationdate="Fri Nov 16 00:59:59 2001"    password="pass" type="2222" status="open" mnemonic="32051981" ratelimit="0">
    <CHECKATTR />
    <REPLYATTR>Service-Type = Frames-User, Framed-Protocol = PPP, Framed-Routing = None</REPLYATTR>
    <SETTINGS bitval="4" status="open" />
    <SETTINGS bitval="8192" status="open" session_timeout="10800" />
    <SETTINGS bitval="32768" status="open" cisco_address_pool="thepool" />
    <ALIASES numaliases="0" />
</ACCOUNT>
</RESPONSE>
</API>

IN this xml, I need to get Settings tag/start element attributes along with it’s values.

These attributes are dynamic, so I am trying to make a map of them. I am new to SAX Parser.

So far my java code:

public void startElement(String s, String s1, String elementName, Attributes attributes) throws SAXException {

    if (elementName.equalsIgnoreCase(GenericConstants.INFO)) {
        this.searchRaidusBean.setServer(attributes.getValue(GenericConstants.SERVER));
        this.searchRaidusBean.setFunction(attributes.getValue(GenericConstants.FUNCTION));
    }
    if (elementName.equalsIgnoreCase(GenericConstants.RESULT)) {
        this.searchRaidusBean.setResultCode(attributes.getValue(GenericConstants.CODE));
    }

    if (elementName.equalsIgnoreCase(GenericConstants.ACCOUNT)) {
        this.searchRaidusBean.setLoginId(attributes.getValue(GenericConstants.LOGIN));
    }
    if (elementName.equalsIgnoreCase(GenericConstants.ACCOUNT)) {
        this.searchRaidusBean.setSkyNum(attributes.getValue(GenericConstants.SKYNUM));
    }
    if (elementName.equalsIgnoreCase(GenericConstants.ACCOUNT)) {
        this.searchRaidusBean.setMaxAliases(attributes.getValue(GenericConstants.MAXALIASES));
    }
    if (elementName.equalsIgnoreCase(GenericConstants.ACCOUNT)) {
        this.searchRaidusBean.setCreationDate(attributes.getValue(GenericConstants.CREATION_DATE));
    }
    if (elementName.equalsIgnoreCase(GenericConstants.ACCOUNT)) {
        this.searchRaidusBean.setType(attributes.getValue(GenericConstants.TYPE));
    }
    if (elementName.equalsIgnoreCase(GenericConstants.ACCOUNT)) {
        this.searchRaidusBean.setStatus(attributes.getValue(GenericConstants.STATUS));
    }
    if (elementName.equalsIgnoreCase(GenericConstants.ACCOUNT)) {
        this.searchRaidusBean.setMnemonic(attributes.getValue(GenericConstants.MNEMONIC));
    }
    if (elementName.equalsIgnoreCase(GenericConstants.ACCOUNT)) {
        this.searchRaidusBean.setRateLimit(attributes.getValue(GenericConstants.RATELIMIT));
    }
    if (elementName.equalsIgnoreCase(GenericConstants.SETTINGS)) {
        //this.searchRaidusBean.getBitval().add(attributes.getValue(GenericConstants.BITVAL));
        System.out.println(attributes);
        //stuck here
    }
    if (elementName.equalsIgnoreCase(GenericConstants.ALIASES)) {
        this.tempKey = attributes.getValue(GenericConstants.MNEMONIC);
    }
}



public void endElement(String str1, String str2, String element) throws SAXException {
    if (element.equalsIgnoreCase(GenericConstants.RESULT)) {
        this.searchRaidusBean.setResultMessage(this.tempValue);
    }
    if (element.equalsIgnoreCase(GenericConstants.ALIASES)) {
        if (!StringUtils.isBlank(this.tempKey)) {
            this.searchRaidusBean.getAlias().put(this.tempKey, this.tempValue);
        }
    }
}


public void characters(char[] charArray, int i, int j) throws SAXException {
    this.tempValue = new String(charArray, i, j);
}
  • 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-10T20:32:37+00:00Added an answer on June 10, 2026 at 8:32 pm

    If you are using the DefaultHandler, then you will be receiving a startElement event.

    This method carries the Attributes as one of it’s parameters.

    You will need to use getIndex(String) to get the index of the named attribute and getValue(int) to get the value of said attribute.

    As Nambari has pointed out, there are hundreds of tutorials on the internet and more then a few posts on the subject on SO (I answered one over the weekend).

    UPDATED

    I’d suggest it should look something like this (I’ve not tested it)

    public void startElement(String uri, String localName, String elementName, Attributes attributes) throws SAXException {
    
        if (elementName.equalsIgnoreCase(GenericConstants.INFO)) {
            this.searchRaidusBean.setServer(attributes.getValue(GenericConstants.SERVER));
            this.searchRaidusBean.setFunction(attributes.getValue(GenericConstants.FUNCTION));
        }
        if (elementName.equalsIgnoreCase(GenericConstants.RESULT)) {
            this.searchRaidusBean.setResultCode(attributes.getValue(GenericConstants.CODE));
        }
    
        if (elementName.equalsIgnoreCase(GenericConstants.ACCOUNT)) {
            this.searchRaidusBean.setLoginId(attributes.getValue(GenericConstants.LOGIN));
            this.searchRaidusBean.setSkyNum(attributes.getValue(GenericConstants.SKYNUM));
            this.searchRaidusBean.setMaxAliases(attributes.getValue(GenericConstants.MAXALIASES));
            this.searchRaidusBean.setCreationDate(attributes.getValue(GenericConstants.CREATION_DATE));
            this.searchRaidusBean.setType(attributes.getValue(GenericConstants.TYPE));
            this.searchRaidusBean.setStatus(attributes.getValue(GenericConstants.STATUS));
            this.searchRaidusBean.setMnemonic(attributes.getValue(GenericConstants.MNEMONIC));
            this.searchRaidusBean.setRateLimit(attributes.getValue(GenericConstants.RATELIMIT));
        }
    
        if (elementName.equalsIgnoreCase(GenericConstants.SETTINGS)) {
    
            for (int index = 0; index < attributes.getLength(); index++) {
    
                String attName = attributes.getLocalName(index);
                String value = attributes.getValue(index);
    
                map.put(attName, value);
    
            }
    
        }
    
        if (elementName.equalsIgnoreCase(GenericConstants.ALIASES)) {
            this.tempKey = attributes.getValue(GenericConstants.MNEMONIC);
        }
    
    }
    

    UPDATED with tested example

    I took you data (from the OP) and run it through the following handler

    DefaultHandler handler = new DefaultHandler() {
        @Override
        public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
    
            if (qName.equalsIgnoreCase("settings")) {
    
                System.out.println("Parse settings attributes...");
    
                for (int index = 0; index < attributes.getLength(); index++) {
    
                    String aln = attributes.getLocalName(index);
                    String value = attributes.getValue(index);
    
                    System.out.println("    " + aln + " = " + value);
    
    
                }
    
            }
    
        }
    };
    

    And I got the following output

    Parse settings attributes...
        bitval = 4
        status = open
    Parse settings attributes...
        bitval = 8192
        status = open
        session_timeout = 10800
    Parse settings attributes...
        bitval = 32768
        status = open
        cisco_address_pool = thepool
    

    So I don’t know what you’re doing.

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

Sidebar

Related Questions

So I'm trying to parse an xml file: <?xml version=1.0 encoding=utf-8 ?> <Root> <att1
I'm trying to learn how to parse an xml file using SAX parser in
I've been trying to use the Java SAX parser to parse an XML file
I am trying to parse an XML file with python using lxml, but get
I am trying to parse an XML file using the SAX interface of libxml2
I am trying to parse xml file and get no errors but when trying
I am trying to parse XML in Perl using XML::SAX parser . My query
I'm trying to parse an xml file that contains accents, but I get this
I have an XML file that I am trying to parse with Sax (this
I am trying XML::SAX parser for parsing XML file. XML file is shown below,

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.