2

I have to make xml document on client side, convert it to byte array and do the reverse sequence on server side.

My xml is:

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<dirStruct>
<file modified="1398855221782" name="sharedFolder\hey.txt"/>
</dirStruct>

... verified with online xml validators, no errors found.

Get it on server side:

byte[] msg = new byte[5000]; // buffer is large enough, so it isn't problem.
in.read(msg);
DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder docBuilder = docFactory.newDocumentBuilder();
Document doc = docBuilder.parse(new ByteArrayInputStream(msg)); // Here's i get an exeption

... input data isn't null, verified up.

Exception is:

[Fatal Error] :1:138: Content is not allowed in trailing section.
org.xml.sax.SAXParseException; lineNumber: 1; columnNumber: 138; Content is not allowed in trailing section.

Where's im wrong?

====================================

Client side:

DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder docBuilder = docFactory.newDocumentBuilder();
Document doc = docBuilder.newDocument();
Element root = doc.createElement("dirStruct");
doc.appendChild(root);

Element node = doc.createElement("file");
node.setAttribute("name", file.toString());
node.setAttribute("modified", Long.toString(file.lastModified()));
root.appendChild(node);

TransformerFactory transformerFactory = TransformerFactory.newInstance();
Transformer transformer = transformerFactory.newTransformer();
DOMSource source = new DOMSource(doc);
StreamResult result = new StreamResult(stream);
transformer.transform(source, result);

... stream is ByteArrayOutputStream.

If i will change stream to System.out, i will get correct result too.

To byte array is just data = stream.toByteArray();

9
  • 1
    What do you get if you make a String from the bytes and print it? Commented Apr 30, 2014 at 11:07
  • @EvanKnowles, absolute copy of client's version. Commented Apr 30, 2014 at 11:12
  • 1
    I think you've already answered your own question. The code isn't the problem, but rather the data sent from client to server. Your question does not explain how the data is sent/received. Commented Apr 30, 2014 at 11:28
  • 1
    It sounds like there're some characters (whitespace perhaps) being inserted. How are you converting from String to byte[] originally? Commented Apr 30, 2014 at 11:30
  • 2
    Could it be an encoding issue? Is the original file created in a Windows machine and sent to a Linux one? Commented Apr 30, 2014 at 11:48

1 Answer 1

3

The buffer is too large. You are getting real content after </dirStruct> as char 0x000000.

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<dirStruct>
<file modified="1398855221782" name="sharedFolder\hey.txt"/>
</dirStruct>¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶
¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶
¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶
¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶

is 141 bytes, there are at least 4859 bytes of 0x00000000.

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

Comments

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.