1

CODE:

public void parse(byte[] payload)
{
    try
    {

        SAXParserFactory factory = SAXParserFactory.newInstance();
        SAXParser saxParser = factory.newSAXParser();

        DefaultHandler handler = new DefaultHandler()
        {
            boolean eid = false;
            boolean msg_id = false;
            boolean date_time = false;
            boolean temp = false;

            public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException
            {
                if (qName.equalsIgnoreCase("eid"))
                {
                    eid = true;
                }

                if (qName.equalsIgnoreCase("msg_id"))
                {
                    msg_id = true;
                }

                if (qName.equalsIgnoreCase("date_time"))
                {
                    date_time = true;
                }

                if (qName.equalsIgnoreCase("temperature"))
                {
                    temp = true;
                }
            }

            public void characters(char ch[], int start, int length) throws SAXException
            {

                if (eid)
                {
                    XMLMessagePacket.this.eid = new String(ch, start, length);
                    eid = false;
                }

                if (msg_id)
                {
                    XMLMessagePacket.this.msgId = new String(ch, start, length);
                    msg_id = false;
                }

                if (date_time)
                {
                    XMLMessagePacket.this.time =  new String(ch, start, length);
                    date_time = false;
                }

                if (temp)
                {
                    XMLMessagePacket.this.temperature.parseDouble(new String(ch, start, length));
                    temp = false;
                }

            }

        };

        saxParser.parse(new ByteArrayInputStream(payload), handler);
    }
    catch (Exception e)
    {
        e.printStackTrace();
    }
}

XML

<?xml version="1.0"?>
<event>
        <eid>345345</eid>
        <msg_id>3242</msg_id>
        <date_time>11342345</date_time>
        <temperature>100</temperature>
</event>

Problem:

org.xml.sax.SAXParseException: Content is not allowed in prolog.

10
  • @TimPietzcker I've went down that route. I've put methods in to check against that and still have the same problem Commented Jan 22, 2013 at 18:46
  • It sounds like your file is invalid - the wrong encoding, perhaps? Where is it coming from? Commented Jan 22, 2013 at 18:50
  • Are you sure there are absolutely no characters preceeding the opening < Commented Jan 22, 2013 at 18:51
  • @JonSkeet I'm passing a byte[] directly to the method which comes in from a udp connection. Weird thing is, new String(payload) prints it correctly Commented Jan 22, 2013 at 18:51
  • 1
    @Mrshll187: This is why it's important to include the actual data. The XML document in your post didn't specify the UTF-16 part. I suspect that the UTF-16 encoding was the actual problem - if there's a UTF-8 byte order mark at the very start of the document it should be okay. Commented Jan 24, 2013 at 20:07

1 Answer 1

1

Just something to check: a friend once fought this "bug" all night. Is it possible your file has a Byte Order Mark at the beginning? Parsing it as a String might make it disappear. The default encoding for XML is UTF-8 (which does not require a BOM). It's just possible you're getting tripped up by something you can't see.

Sign up to request clarification or add additional context in comments.

2 Comments

This was exactly what was causing it
UTF-8 doesn't require a byte order mark, but it's valid to have one. It sounds like the UTF-16 problem was the bigger issue here. See w3.org/TR/2008/REC-xml-20081126/#charencoding

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.