SAXException:尾随部分中不允许包含内容



这让我发疯了。我已经在许多不同的项目中使用了这段代码,但这是它第一次给我这种类型的错误。这是整个 XML 文件:

  <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>

以下是我的自制 Xml 类中的操作代码,它使用 DocumentBuilderFactory 来解析输入到其中的 Xml :

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;
    }

我正在使用它的上下文是:制作Photoshop类型图像处理应用程序的学校项目。该文件与图层一起保存为.png,此 xml 文件用于.zip文件中图层的位置等。我不知道拉链是否增加了一些神秘的额外字符。

感谢您的反馈。

如果您在编辑器中查看该文件,您将看到结束元素后面的内容(可能是空格),例如

</layers>  <-- after here

值得使用突出显示空格字符的工具将其转储出来,例如

$ cat -v -e my.xml

将转储"不可打印"字符。

希望这在某些时候对某人有所帮助。有效的修复程序只是将lastIndexOf()与子字符串一起使用。这是原位代码:

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();
   }

}

感谢所有做出贡献的人。我怀疑该错误是由zip进程或使用byte[]缓冲区引入的。任何进一步的反馈不胜感激。

我在XML的末尾有一些额外的字符,请正确检查XML或执行XML的在线格式,如果XML不正确,则会引发错误。我用了联机 XML 格式化程序

我刚刚在 Sterling Integrator 中遇到了这个错误 - 当我在十六进制编辑器中查看文件时它有大约 5 行额外的 char(0),而不是空格。不知道它们来自哪里,但这正是问题所在,特别是当我基本上在进行统一转换时,所以 xsl 引擎 - 在本例中为 xalan - 显然将其传递到结果中。删除了最后一个关闭尖括号后的额外行,问题解决了。

最新更新