Java Plist XML解析 [英] Java Plist XML Parsing
问题描述
我正在用Java解析(格式不正确的)Apple Plist文件.
I'm parsing a (not well formed) Apple Plist File with java.
我的代码如下:
InputStream in = new FileInputStream( "foo" );
XMLInputFactory factory = XMLInputFactory.newInstance();
XMLEventReader parser = factory.createXMLEventReader( in );
while (parser.hasNext()){
XMLEvent event = parser.nextEvent();
//code to navigate the nodes
}
我正在解析的部分看起来像这样:
The parts I"m parsing are looking like this:
<dict>
<key>foo</key><integer>123</integer>
<key>bar</key><string>Boom & Shroom</string>
</dict>
现在我的问题是,包含与号的节点未按应有的方式进行解析,因为与"号代表一个实体.
My problem is now, that nodes containing a ampersand are not parsed like they should because the ampersand is representing a entity.
我该怎么做才能将节点的值作为完整的String而不是损坏的部分得到?
What can i do to get the value of the node as a complete String, instead of broken parts?
谢谢.
推荐答案
您应该可以通过在XMLInputFactory
上设置IS_COALESCING
属性来解决您的问题(相对于XMLEventReader
,我也更喜欢XMLStreamReader
,但是ymmv):
You should be able to solve your problem by setting the IS_COALESCING
property on the XMLInputFactory
(I also prefer XMLStreamReader
over XMLEventReader
, but ymmv):
XMLInputFactory factory = XMLInputFactory.newInstance();
factory.setProperty(XMLInputFactory.IS_COALESCING, Boolean.TRUE);
InputStream in = // ...
xmlReader = factory.createXMLStreamReader(in, "UTF-8");
顺便说一下,据我所知,没有一个JDK解析器会处理格式不正确"的XML而不会造成阻塞.实际上,您的XML格式正确:它使用实体而不是原始的&符号.
Incidentally, to the best of my knowledge none of the JDK parsers will handle "not well formed" XML without choking. Your XML is, in fact, well-formed: it uses an entity rather than a raw ampersand.
这篇关于Java Plist XML解析的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!