mitchguy
asked on
sending xml message in a java udp packet
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,buff er.length, InetAddres s.getByNam e("127.0.0 .1"),Integ er.parseIn t("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?
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,buff
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?
ASKER
The reader is doing this
DatagramPacket incomingPacket = new DatagramPacket(buffer,buff er.length) ;
listenSocket.receive(incom ingPacket) ;
byte data[] = incomingPacket.getData();
InputSource source = new InputSource(new StringReader(data.toString ()));
fromXML(source);
private synchronized fromXML(InputSource in) throws Exception
{
XMLReader xr;
xr.setContentHandler(this) ;
xr.parse(in);
}
DatagramPacket incomingPacket = new DatagramPacket(buffer,buff
listenSocket.receive(incom
byte data[] = incomingPacket.getData();
InputSource source = new InputSource(new StringReader(data.toString
fromXML(source);
private synchronized fromXML(InputSource in) throws Exception
{
XMLReader xr;
xr.setContentHandler(this)
xr.parse(in);
}
>>InputSource source = new InputSource(new StringReader(data.toString ()));
should be as below
should be as below
InputSource source = new InputSource(new StringReader(new String(data)));
ASKER
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.SAXParseExcept ion: 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?
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.SAXParseExcept
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?
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);
System.out.println(data);
InputSource source = new InputSource(new StringReader(s));
ASKER
The output is certainly not what I expected
[B@1113708
Since I'm sending and receiving on the same machine it shouldn't be a byte swap problem right?
[B@1113708
Since I'm sending and receiving on the same machine it shouldn't be a byte swap problem right?
ASKER CERTIFIED SOLUTION
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
ASKER
If I do this
String myString = new String(incomingPacket.getD ata());
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.SAXParseExcept ion: Content is not allowed in prolog
String myString = new String(incomingPacket.getD
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.SAXParseExcept
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
ASKER
That did the Trick:)
Thanks!
Thanks!
:-)
That sounds more like an error with reading xml, not writing it. At what point are you getting that message