这让我疯狂。我已经将这些代码用于很多不同的项目,但这是第一次给我这种类型的错误。这是整个XML文件:SAXException:内容不允许在尾部
<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文件中图层的位置等。我不知道压缩是否添加了一些神秘的额外字符。
我感谢您的反馈。
你是否100%确定你发布的内容确实是你的XML文件的内容?你的调试器对xmlString变量的内容有什么看法?你可能有多个根元素?您也可能想要发布生成'xmlString'内容的代码。 – predi
感谢您的回答。我同意GenericJam - 它与缓冲区有关。默认情况下,所有字节都是0(零),对'zipFile.getInputStream(xmlZipFile).read(buffer)'的调用将覆盖这些字节的第一个'n'。但是,在XML文档中,尾随的0字节将被视为“内容”。 – welterw8