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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T23:54:29+00:00 2026-05-26T23:54:29+00:00

I am trying to write a simple SAX parser, I am receiving the inputs

  • 0

I am trying to write a simple SAX parser, I am receiving the inputs from a Web service response, and it includes escape characters < and > When I am trying to parse it using my code, I am getting Reference is not allowed in prolog. Error, where as if I change the escape characters to normal < and > character it is parsing without any issues, I guess I am just missing something very simple here… can somebody please help?

import java.io.ByteArrayInputStream;
import java.io.FileReader;
import java.io.InputStream;

import org.xml.sax.XMLReader;
import org.xml.sax.Attributes;
import org.xml.sax.InputSource;
import org.xml.sax.helpers.XMLReaderFactory;
import org.xml.sax.helpers.DefaultHandler;

public class Test extends DefaultHandler {

    public static void main(String args[]) throws Exception {
        XMLReader xr = XMLReaderFactory.createXMLReader();
        Test handler = new Test();
        xr.setContentHandler(handler);
        xr.setErrorHandler(handler);

        String xml_string = "&lt;rootnode&gt;&lt;a&gt;hello&lt;/a&gt;&lt;b&gt;world&lt;/b&gt;&lt;/rootnode&gt;";
        InputStream xmlStream = new ByteArrayInputStream(xml_string.getBytes("UTF-8"));
        xr.parse(new InputSource(xmlStream));
    }

    public Test() {
        super();
    }

    ////////////////////////////////////////////////////////////////////
    // Event handlers.
    ////////////////////////////////////////////////////////////////////

    public void startDocument() {
        System.out.println("Start document");
    }

    public void endDocument() {
        System.out.println("End document");
    }

    public void startElement(String uri, String name, String qName, Attributes atts) {
        if ("".equals(uri))
            System.out.println("Start element: " + qName);
        else
            System.out.println("Start element: {" + uri + "}" + name);
    }

    public void endElement(String uri, String name, String qName) {
        if ("".equals(uri))
            System.out.println("End element: " + qName);
        else
            System.out.println("End element:   {" + uri + "}" + name);
    }

    public void characters(char ch[], int start, int length) {
        System.out.print("Characters:    \"");
        for (int i = start; i < start + length; i++) {
            switch (ch[i]) {
            case '\\':
                System.out.print("\\\\");
                break;
            case '"':
                System.out.print("\\\"");
                break;
            case '\n':
                System.out.print("\\n");
                break;
            case '\r':
                System.out.print("\\r");
                break;
            case '\t':
                System.out.print("\\t");
                break;
            default:
                System.out.print(ch[i]);
                break;
            }
        }
        System.out.print("\"\n");
    }
}
  • 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-26T23:54:29+00:00Added an answer on May 26, 2026 at 11:54 pm

    You shouldn’t be using escape characters in your xmlstring. You need to use < and > for the xml tags. Only escape them when you need to include < or > as part of the content of an attribute/element not the element tag itself.

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

Sidebar

Related Questions

I'm trying to write a simple WPF application in C#. Coming from a primarily
I'm trying write a simple perl script that reads some fields from a password
Hi trying to write a simple linq query from a tutorial I read. But
I'm trying to write a simple python web application using the Tornado web server
I am trying to write a simple stream, streaming everything from a mysql field.
I am trying to write a simple Ruby program that I will run from
Im trying to write very simple HTML parser with ANTLR and Im facing problem,
I am trying to write a simple sql parser to break a sql statement
I'm trying to write a simple program to calculate betweeness using brandes_betweenness_centrality from boostlib.
I am trying to write simple Visual Studio Add-In for code generation. In my

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.