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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T12:27:30+00:00 2026-05-23T12:27:30+00:00

Given this XML file: <?xml version=1.0 encoding=UTF-8?> <root> <data> <track clipid=1> <url>http://www.emp3world.com/to_download.php?id=33254</url> <http_method>GET or

  • 0

Given this XML file:

<?xml version="1.0" encoding="UTF-8"?>
<root>
   <data>
      <track clipid="1">
         <url>http://www.emp3world.com/to_download.php?id=33254</url>
         <http_method>GET or POST</http_method>
         <post_body>a=1&b=2&c=3</post_body>
      </track>
   </data>
</root>

What I am after is to print something like this from this XML file:

ID: 1
URL: http://www.emp3world.com/to_download.php?id=33254
Http method: GET or POST

At the moment this is my primitive handler code:

class MyHandler extends DefaultHandler
{
    String str = "";
    StringBuilder s = new StringBuilder();
    public void startElement(String namespaceURI, String sName, String qName, Attributes atts)
    {
        if(qName.equals("track"))
        {
            s.append("ID: ").append(atts.getValue("clipid")).append("\n");
        }
        if(qName.equals("url"))
        {
            s.append("URL: ");
        }
        if(qName.equals("http_method"))
        {
            s.append("Http method: ");
        }
    }

    public void endElement(String uri, String localName, String qName)
    {
        if(qName.equals("url"))
        {
            s.append(str).append("\n");
            str = "";
        }
        if(qName.equals("http_method"))
        {
            s.append(str).append("\n");
            str = "";
        }
        System.out.println(s);
    }

    public void characters(char[] ch, int start, int length) throws SAXException {
        str = new String(ch, start, length);
    }
}

My problem is that it always prints the results 4 times(first time without the Http Method field. I guess this is a problem for all Sax Parsers beginners.
I know what startElement, endElement, characters functions do, but as you can see, I don’t know how to use them correctly. What should I change in my code so I can have the correct output?

  • 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-23T12:27:31+00:00Added an answer on May 23, 2026 at 12:27 pm

    The problem is your characters method. Change its body to

    s.append(new String(ch, start, length));
    

    then add this line to the start of startElement

    s.setLength(0);
    

    and you should see some output.

    Here’s what the Java tutorial on SAX has to say about the characters method:

    Parsers are not required to return any particular number of characters at one time. A parser can return anything from a single character at a time up to several thousand and still be a standard-conforming implementation. So if your application needs to process the characters it sees, it is wise to have the characters() method accumulate the characters in a java.lang.StringBuffer and operate on them only when you are sure that all of them have been found.

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

Sidebar

Related Questions

Given this XML: <?php $xml = <<<XML <?xml version=1.0 encoding=UTF-8 ?> <root> <project type=residential>
Given this xml document: <?xml version=1.0 encoding=UTF-8?> <mydoc> <foo f=fooattr> <bar r=barattr1> <baz z=bazattr1>this
I can't quite get my query correct. Given this XML: <?xml version=1.0 encoding=utf-8 ?>
Say I have this given XML file: <root> <node>x</node> <node>y</node> <node>a</node> </root> And I
html Tidy gives this as output for some reason: <?xml version=1.0 encoding=utf-16?> <?xml version=1.0
I have a xml file <?xml version=1.0 encoding=UTF-8?> <xml> <settings> <title>Calendar for September</title> <subTitle>Calendar
Given this XML ... <ListBucketResult xmlns=http://s3.amazonaws.com/doc/2006-03-01/> <Name>public.rpmware.com</Name> <Prefix></Prefix> <Marker></Marker> <MaxKeys>1000</MaxKeys> <IsTruncated>false</IsTruncated> <Contents> <Key>0.dir</Key> <LastModified>2008-06-25T16:09:49.000Z</LastModified>
Given this XML, what XPath returns all elements whose prop attribute contains Foo (the
Given this data set: ID Name City Birthyear 1 Egon Spengler New York 1957
Given this algorithm, I would like to know if there exists an iterative version.

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.