【问题标题】:SAX Parser - Extract string within tagsSAX Parser - 在标签中提取字符串
【发布时间】:2014-01-23 02:34:45
【问题描述】:

这是我的问题:我需要使用 SAX Parser 提取标签“p”之间的文本,而不使用 XML 表示法

    <title>1. Introduction</title>
    <p>The Lorem ipsum 
           <xref ref-type="bibr" rid="B1">
                1
           </xref>. 
           Lorem ipsum 23.
     </p>
     <p>The L domain recruits an ATP-requiring cellular factor for this 
           scission event, the only known energy-dependent step in assembly 
           <xref ref-type="bibr" rid="B2">
                2
           </xref>. 
           Domain is used here to denote the amino 
           acid sequence that constitutes the biological function.
     </p>

是否可以使用 endElement() ?因为当我使用它时,我只获得“/xref”标签之后的部分

这里是代码

public void endElement(String s, String s1, String element) throws SAXException {

        if(element.equals(Finals.PARAGRAPH)){
            Paragraph paragraph = new Paragraph();
            paragraph.setContext(tmpValue);
            System.out.println("Contesto: " + tmpValue);
            listP.add(paragraph);

        }
    }
    @Override
    public void characters(char[] ac, int i, int j) throws SAXException {
        tmpValue = new String(ac, i, j);

    }

这是我期望做的:包含两个段落的列表listP

1) Lorem ipsum 1 Lorem ipsum 23.
2) The L domain recruits an ATP-requiring cellular factor for this 
       scission event, the only known energy-dependent step in assembly 2 
       Domain is used here to denote the amino 
       acid sequence that constitutes the biological function.

【问题讨论】:

  • 因为显然endElement 是在...结束元素上调用的。您对名为 CDATA 的部分感兴趣。您应该为此找到合适的处理程序。你应该使用你的实际代码来展示你当前的尝试。
  • 看来你做得很好。问题出在哪里?
  • 我需要这个结果The L domain recruits an ATP-requiring cellular factor for this scission event, the only known energy-dependent step in assembly 2. Domain is used here to denote the amino acid sequence that constitutes the biological function.,但我只得到Domain is used here to denote the amino acid sequence that constitutes the biological function.

标签: java xml parsing saxparser


【解决方案1】:

我不确定您所说的“是否可以使用 endElement”是什么意思,但它肯定是可能的。您需要编写 SAX 应用程序:

(1) 忽略&lt;p&gt;aragraph 之间的所有startElement/endElement 事件——简单的状态跟踪,或者您可以简单地说您对段落以外的元素不感兴趣并制作您的元素事件处理程序对您不关心的任何事情都是无操作的。

(2) 累积单独传递的characters() 事件,直到endElement&lt;p&gt;aragraph。但无论如何您都需要这样做,因为 SAX始终保留将连续文本作为多个 characters() 调用传递的权利,原因与解析器缓冲区管理有关。

【讨论】:

    【解决方案2】:

    有许多可能的解决方案。通常使用 SAX 解析器,您只需添加一些布尔标志来表示解析时的某些特定状态。在这个简单的例子中,你可以通过改变这个来实现:

    tmpValue = new String(ac, i, j);
    

    到这里:

    if (tmpValue.equals(""))
        tmpValue = new String(ac, i, j);
    else
        tmpValue += new String(ac, i, j);
    

    或:

    if (tmpValue == null)
        tmpValue = new String(ac, i, j);
    else
        tmpValue += new String(ac, i, j);
    

    取决于您如何初始化 tmpValue 变量(如果您还没有这样做,您应该初始化它)。

    要收集所有段落的内容,您需要:

    public void endElement(String s, String s1, String element) throws SAXException {
    
        if (element.equals(Finals.PARAGRAPH)) {
            Paragraph paragraph = new Paragraph();
            paragraph.setContext(tmpValue);
            System.out.println("Contesto: " + tmpValue);
            listP.add(paragraph);
            tmpValue = ""; // or tmpValue = null; for the second version
        }
    }
    

    并省略标题部分:

    public void startElement(
        String uri,
        String localName,
        String qName,
        Attributes atts) {
    
        if (localName.equals(Finals.PARAGRAPH)) {
            tmpValue = ""; // or tmpValue = null; for the second version
        }
    }
    

    【讨论】:

    • 我得到 nullPointerException 添加该解决方案。
    • @user3162945 请具体说明。我提供了两种解决方案。正如我所建议的,是否也初始化了tmpValue
    • 我忘记初始化tmpValue。现在可以工作,但我没有得到完整的字符串。只有/xref之后的部分
    • @user3162945 我误解了您的初始要求。检查此编辑。
    • 它可以工作,但它需要 xml 中的所有内容。我只需要p 标签中的部分。检查我的编辑。
    【解决方案3】:

    startElement 事件中使用stack
    Push,在endElement 事件中使用Pop

    或者,如果这对您不起作用,只需将 Push 放入所有事件的堆栈中,然后在 endOfDocumentPop 之后逐个元素。反向存储&lt;/p&gt;&lt;p&gt;的数据。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-07-06
      • 2013-08-29
      • 2011-12-18
      • 1970-01-01
      • 2023-04-03
      • 1970-01-01
      相关资源
      最近更新 更多