Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SAXException: Content is not allowed in trailing section

This is driving me crazy. I have used this bit of code for lots of different projects but this is the first time it's 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 operative bit of code in my homebrew Xml class which uses the DocumentBuilderFactory to parse the Xml fed 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 that I am using it is: school project to produce a Photoshop type image manipulation application. The file is being saved with the layers as .png and this xml file for the position, etc. of the layers in a .zip file. I don't know if the zipping is adding some mysterious extra characters or not.

I appreciate your feedback.

like image 795
GenericJam Avatar asked Dec 03 '12 11:12

GenericJam


People also ask

Is content allowed in trailing section when parsing with sax?

xml - "Content is not allowed in trailing section." when parsing with SAX java - Stack Overflow "Content is not allowed in trailing section." when parsing with SAX java Bookmark this question.

What is sax error – content not allowed in Prolog?

In short, invalid text or BOM before the XML declaration or different encoding will cause the SAX Error – Content is not allowed in prolog. 1. Invalid text before the XML declaration.

Why content is not allowed in training section in Salesforce?

Org.xml.sax.saxparseexception: content is not allowed in training section To sum up, there are two main reasons for this problem 1. Incorrect XML content, such as multiple spaces, line feed, etc.


2 Answers

If you look at that file in an editor, you'll see content (perhaps whitespace) following the end element e.g.

</layers>  <-- after here

It's worth dumping this out using a tool that will highlight whitespace chars e.g.

$ cat -v -e my.xml

will dump 'unprintable' characters.

like image 108
Brian Agnew Avatar answered Sep 22 '22 19:09

Brian Agnew


Hopefully this can be helpful to someone at some point. The fix that worked was just to use lastIndexOf() with substring. Here's the code in situ:

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 for all the people that contributed. I suspect the error was either introduced by the zip process or by using the byte[] buffer. Any further feedback is appreciated.

like image 42
GenericJam Avatar answered Sep 24 '22 19:09

GenericJam