在 Java 中解析 XML 文件中的文本值

发布于 2024-11-18 20:30:48 字数 648 浏览 6 评论 0原文

现在我正在使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

囍孤女 2024-11-25 20:30:48

阅读这篇关于 SAX 解析的文章(它是旧但仍然有效),请特别注意 characters 方法是如何实现的。这是非常不直观的,并且会让每个人都感到困惑,你会因为看似没有充分理由的原因而多次调用字符

另外,SAX 的 Java 教程 对这些字符进行了简短说明方法:

解析器不需要一次返回任何特定数量的字符。解析器可以一次返回从单个字符到数千个字符的任何内容,并且仍然是符合标准的实现。因此,如果您的应用程序需要处理它看到的字符,那么明智的做法是让 strings() 方法将字符累积在 java.lang.StringBuffer 中,并仅在您确定已找到所有字符时才对它们进行操作。< /p>

在您的情况下(没有混合内容的 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 to characters for what seems like no good reason.

Also the Java tutorial on SAX has a short explanation of the characters method:

Parsers are not required to return any particular number of characters at one time. A parser can return anything from a single character at a time up to several thousand and still be a standard-conforming implementation. So if your application needs to process the characters it sees, it is wise to have the characters() method accumulate the characters in a java.lang.StringBuffer and operate on them only when you are sure that all of them have been found.

In your case (XML with no mixed-content) that means storing the results of multiple characters() calls until the next call to endElement.

北方。的韩爷 2024-11-25 20:30:48

请参阅characters() ContentHandler 方法。仔细阅读 javadoc - 当您只期望一个调用时,您可能会收到多个调用。

See the characters() ContentHandler method. Read the javadoc carefully - you can get multiple calls when you might expect only one.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文