此代码用于从其字符串表示形式生成 XML 文档。它在我的小型单元测试中工作正常,但在我的实际 xml 数据中失败。触发的行是Document doc = db.parse(is);
有任何想法吗?
public static Document FromString(String xml)
{
// from http://www.rgagnon.com/javadetails/java-0573.html
try
{
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
InputSource is = new InputSource();
is.setCharacterStream(new StringReader(xml));
Document doc = db.parse(is);
doc.normalize();
return doc;
}
catch (Exception e)
{
Log.WriteError("Failed to parse XML", e, "XML.FromString(String)");
return null;
}
}
谢谢大家的帮助。
我丢弃了<?xml version="1.0" encoding="utf-8"?>
这清除了这个错误。仍然不明白这可能是什么原因,但它仍然有效。
我继续发现我的一个缓冲写入器(当从 zip 文件提取到内存中时)没有被刷新,这导致 xml 字符串不完整。
感谢大家的帮助!
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)