sending xml message in a java udp packet

Posted on 2008-06-12
Last Modified: 2013-11-23
I'm trying to send an xml message to a Java SAX parser listening on a UDP port.
When I send a message the parser it gives the error message

 [Fatal Error] :1:1 Content is not allowed in prolog.

I've reduced my UDP packet to just the prolog and I still get the error

Here is what I'm sending

String xmlMsg = new String("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");

byte buffer[] = xmlMsg.getBytes();
DatagramSocket sndSocket = new DatagramSocket();
sndSocket.send(new DatagramPacket(buffer,buffer.length,InetAddress.getByName(""),Integer.parseInt("3031")));

I want to find out what I'm doing wrong and how do I send an XML message in a UDP packet that the SAX parser will accept?
Question by:mitchguy
  • 6
  • 5
LVL 86

Expert Comment

ID: 21772083
>> [Fatal Error] :1:1 Content is not allowed in prolog.

That sounds more like an error with reading xml, not writing it. At what point are you getting that message

Author Comment

ID: 21772265
The reader is doing this

DatagramPacket incomingPacket = new DatagramPacket(buffer,buffer.length);
byte data[] = incomingPacket.getData();
InputSource source = new InputSource(new StringReader(data.toString()));

private synchronized fromXML(InputSource in) throws Exception
  XMLReader xr;
LVL 86

Expert Comment

ID: 21772450
>>InputSource source = new InputSource(new StringReader(data.toString()));

should be as below
InputSource source = new InputSource(new StringReader(new String(data)));

Open in new window


Author Comment

ID: 21772538
I'm not sure I understand the difference between data.toString() and new String(data) ?

I made the change and ran it again I get this error listed below still

[Fatal Error] :1:39: Content is not allowed in prolog.
org.xml.sax.SAXParseException: Content is not allowed in prolog

Which is actually a little different than before, the difference being
before the numbers after [Fatal Error] were 1:1 and now it is 1:39
The size of the message is 38 maybe I have some sort of missing character
that is supposed to be at the end of the data for the parser?
LVL 86

Expert Comment

ID: 21772641
Well  byte[] doesn't have a toString method that will be of any use to you. Can you instead do this and tell us what gets printed?
String s = new String(data);


InputSource source = new InputSource(new StringReader(s));

Open in new window

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!


Author Comment

ID: 21772798
The output is certainly not what I expected


Since I'm sending and receiving on the same machine it shouldn't be a byte swap problem right?
LVL 86

Accepted Solution

CEHJ earned 125 total points
ID: 21772904

That looks suspiciously like the output of data.toString, which i've already said not to use ..;-)

Author Comment

ID: 21773164
If I do this
String myString = new String(incomingPacket.getData());
The output is
<?xml version="1.0" encoding="UTF-8"?>

However after that I did
InputSource source = new InputSource(new StringReader(myString));

and unfortunately still got the error
[Fatal Error] :1:39: Content is not allowed in prolog.
org.xml.sax.SAXParseException: Content is not allowed in prolog
LVL 86

Expert Comment

ID: 21773306
Well of course your software is expecting to parse a document. 'myString' is just a header. You need the rest of the doc in there too

Author Comment

ID: 21773433
That did the Trick:)
LVL 86

Expert Comment

ID: 21773461

Featured Post

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
PHP and Soap 3 27
backtracking recursion  code 19 40
Convert Oracle data into XML document 2 37
JList custom Cell Renderer refresh 15 39
For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:

707 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now