在 Java 中解析 XML 文件中的文本值
现在我正在使用 Java 中的 SAX 解析器来解析位于 .docx 文件存档中的“document.xml”文件。下面是我试图解析的示例...
示例 XML 文档
<w:pStyle w:val="Heading2" />
</w:pPr>
<w:bookmarkStart w:id="0" w:name="_Toc258435889" />
<w:bookmarkStart w:id="1" w:name="_Toc259085121" />
<w:bookmarkStart w:id="2" w:name="_Toc259261685" />
- <w:r w:rsidRPr="00415FD6">
<w:t>Text To Extract</w:t>
</w:r>
<w:bookmarkEnd w:id="0" />
<w:bookmarkEnd w:id="1" />
<w:bookmarkEnd w:id="2" />
现在,我知道如何取出属性值,这并不难。但是,我不知道如何进入并解析节点内的实际文本。有人对此有任何想法或经验吗?先感谢您。
So right now I am using the SAX parser in Java to parse the "document.xml" file located within a .docx file's archive. Below is a sample of what I am trying to parse...
Sample XML Document
<w:pStyle w:val="Heading2" />
</w:pPr>
<w:bookmarkStart w:id="0" w:name="_Toc258435889" />
<w:bookmarkStart w:id="1" w:name="_Toc259085121" />
<w:bookmarkStart w:id="2" w:name="_Toc259261685" />
- <w:r w:rsidRPr="00415FD6">
<w:t>Text To Extract</w:t>
</w:r>
<w:bookmarkEnd w:id="0" />
<w:bookmarkEnd w:id="1" />
<w:bookmarkEnd w:id="2" />
Right now, I know how to take out attribute values, that's not hard. However, I do not know how to get in and parse the actual text within the nodes. Does anyone have any ideas or prior experience with this? Thank you in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
阅读这篇关于 SAX 解析的文章(它是旧但仍然有效),请特别注意
characters
方法是如何实现的。这是非常不直观的,并且会让每个人都感到困惑,你会因为看似没有充分理由的原因而多次调用字符
。另外,SAX 的 Java 教程 对这些字符进行了简短说明方法:
在您的情况下(没有混合内容的 XML),这意味着存储多个character() 调用的结果,直到下一次调用 endElement。
Read this article on SAX parsing (it is old but still valid), pay particular attention to how the
characters
method is implemented. It is very unintuitive and trips everybody up, you will get multiple calls tocharacters
for what seems like no good reason.Also the Java tutorial on SAX has a short explanation of the characters method:
In your case (XML with no mixed-content) that means storing the results of multiple characters() calls until the next call to endElement.
请参阅characters() ContentHandler 方法。仔细阅读 javadoc - 当您只期望一个调用时,您可能会收到多个调用。
See the characters() ContentHandler method. Read the javadoc carefully - you can get multiple calls when you might expect only one.