Reputation: 2367
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();
Upvotes: 2
Views: 812
Reputation: 2040
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.
Upvotes: 3