SAXException: content not allowed in trailing section

It drives me crazy. I used this bit of code for many different projects, but this is the first time that he has given me this type of error. This is the whole XML file:

<layers> <layer name="Layer 1" h="400" w="272" z="0" y="98" x="268"/> <layer name="Layer 0" h="355" w="600" z="0" y="287" x="631"/> </layers> 

Here is the online bit of code in my homebrew Xml class that uses DocumentBuilderFactory to parse the Xml loaded into it:

 public static Xml parse(String xmlString) { DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); dbf.setNamespaceAware(true); Document doc = null; //System.out.print(xmlString); try { doc = dbf.newDocumentBuilder().parse( new InputSource(new StringReader(xmlString))); // Get root element... Node rootNode = (Element) doc.getDocumentElement(); return getXmlFromNode(rootNode); } catch (ParserConfigurationException e) { System.out.println("ParserConfigurationException in Xml.parse"); e.printStackTrace(); } catch (SAXException e) { System.out.println("SAXException in Xml.parse "); e.printStackTrace(); } catch (IOException e) { System.out.println("IOException in Xml.parse"); e.printStackTrace(); } return null; } 

The context I use is: a school project to create an application for manipulating images like Photoshop. The file is saved with layers as .png and this xml file for position, etc. Layers in a .zip file. I don't know if zipping adds some mysterious extra characters or not.

I appreciate your feedback.

+4
source share
4 answers

Hope this can help someone at some point. The fix that worked was to simply use lastIndexOf () with a substring. Here is the in situ code:

 public void loadFile(File m_imageFile) { try { ZipFile zipFile = new ZipFile(m_imageFile); ZipEntry xmlZipFile = zipFile.getEntry("xml"); byte[] buffer = new byte[10000]; zipFile.getInputStream(xmlZipFile).read(buffer); String xmlString = new String(buffer); Xml xmlRoot = Xml.parse(xmlString.substring(0, xmlString.lastIndexOf('>')+1)); for(List<Xml> iter = xmlRoot.getNestedXml(); iter != null; iter = iter.next()) { String layerName = iter.element().getAttributes().getValueByName("name"); m_view.getCanvasPanel().getLayers().add( new Layer(ImageIO.read(zipFile.getInputStream(zipFile.getEntry(layerName))), Integer.valueOf(iter.element().getAttributes().getValueByName("x")), Integer.valueOf(iter.element().getAttributes().getValueByName("y")), Integer.valueOf(iter.element().getAttributes().getValueByName("w")), Integer.valueOf(iter.element().getAttributes().getValueByName("h")), Integer.valueOf(iter.element().getAttributes().getValueByName("z")), iter.element().getAttributes().getValueByName("name")) ); } zipFile.close(); } catch (FileNotFoundException e) { System.out.println("FileNotFoundException in MainController.loadFile()"); e.printStackTrace(); } catch (IOException e) { System.out.println("IOException in MainController.loadFile()"); e.printStackTrace(); } 

}

Thanks to all the people who contributed. I suspect that the error was either introduced by the zip process, or using the byte [] buffer. Any further feedback is appreciated.

+5
source

If you look at this file in the editor, you will see the contents (possibly a space) after the final element, for example.

 </layers> <-- after here 

It is worth dropping this with a tool that will highlight space characters, for example.

 $ cat -v -e my.xml 

will unload "non-printable" characters.

+9
source

I just had this error in Sterling Integrator - when I looked at the file in the hex editor, it had about 5 additional char (0) lines, and not a space. I donโ€™t know where they came from, but that was the problem, especially since I basically did a unity conversion, so the xsl engine - in this case xalan - explicitly passed it to the result. Additional lines after the last bracket of the closed corner are deleted, the problem is solved.

0
source

I had a few extra char at the end of the XML, validate the XML correctly, or make an online XML format that will throw an error if XML does not fit. I used the Online XML Formatter

0
source

All Articles