java - 从 xml 获取正确元素时遇到问题

标签 java xml dom

我有以下 xml:

 <TEI>
  <xi:include href="header.xml"/>
  <text>
   <body>
    <!-- morph_1-p is akapit 7300 with instances (akapit_transzy-s) 14598, 14618 in batches (transza-s) 1461, 1463 resp. -->
    <p corresp="ann_segmentation.xml#segm_1-p" xml:id="morph_1-p">
     <s corresp="ann_segmentation.xml#segm_1.35-s" xml:id="morph_1.35-s">
      <seg corresp="ann_segmentation.xml#segm_1.1-seg" xml:id="morph_1.1-seg">
       <fs type="morph">
        <f name="orth">
         <string>Sami</string>
        </f>
        <!-- Sami [0,4] -->
        <f name="interps">
         <fs type="lex" xml:id="morph_1.1.1-lex">
          <f name="base">
           <string>sam</string>
          </f>
          <f name="ctag">
           <symbol value="adj"/>
          </f>
          <f name="msd">
           <vAlt>
            <symbol value="pl:nom:m1:pos" xml:id="morph_1.1.1.1-msd"/>
            <symbol value="pl:voc:m1:pos" xml:id="morph_1.1.1.2-msd"/>
           </vAlt>
          </f>
         </fs>
        </f>
        <f name="disamb">
         <fs feats="#an8003" type="tool_report">
          <f fVal="#morph_1.1.1.1-msd" name="choice"/>
          <f name="interpretation">
           <string>sam:adj:pl:nom:m1:pos</string>
          </f>
         </fs>
        </f>
       </fs>
      </seg>

在此 xml 中,仅重复节点(所有父节点仅出现一次)

我正在尝试获取:

        <f name="orth">
         <string>Sami</string>
        </f>

和:

          <f name="interpretation">
           <string>sam:adj:pl:nom:m1:pos</string>
          </f>

整个 xml 中没有缺少这些的情况。

这是我的代码:

    InputStream inputStream = new FileInputStream(file);
    Reader inputStreamReader = new InputStreamReader(inputStream, "UTF-8");
    InputSource inputSource = new InputSource(inputStreamReader);
    inputSource.setEncoding("UTF-8");
    DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
    DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder();
    Document document = documentBuilder.parse(inputSource);
    document.getDocumentElement().normalize();
    NodeList nodeListSeg = document.getElementsByTagName("seg");
    for(int i = 0; i < nodeListSeg.getLength(); i++) {
        if(nodeListSeg.item(i).getFirstChild().getFirstChild().getNodeType() == Node.ELEMENT_NODE)
            words.add(((Element) nodeListSeg.item(i).getFirstChild().getFirstChild()).getTextContent().trim());
        if(nodeListSeg.item(i).getLastChild().getNodeType() == Node.ELEMENT_NODE)
            words.add(((Element) nodeListSeg.item(i).getLastChild()).getTextContent().trim());
    }
    inputStreamReader.close();
    inputStream.close();

我尝试的另一种方法是检查属性值:

if(((Element) nodeListSeg.item(i).getFirstChild()).getAttribute("name").equals("orth")) {...}
if(((Element) nodeListSeg.item(i).getFirstChild()).getAttribute("name").equals("interpretation")) 

但是这种比较永远不会返回 true。

最佳答案

解决方案是这样的:

NodeList nodeListSeg = document.getElementsByTagName("seg");
for(int i = 0; i < nodeListSeg.getLength(); i++) {
    NodeList nodeListChildren = nodeListSeg.item(i).getChildNodes();
    for(int j = 0; j < nodeListChildren.getLength(); j++) {
        if(nodeListChildren.item(j).getNodeType() == Node.ELEMENT_NODE) {
            String text = ((Element) nodeListChildren.item(j)).getTextContent().toLowerCase().trim();
            String[] stringArray = text.split(" ");
            System.out.println(stringArray[0] + "\t" + stringArray[stringArray.length - 1]);
        }
    }
}

因此,FS 节点中的所有 text 节点均未正确解析,并且全部被视为单个元素。

关于java - 从 xml 获取正确元素时遇到问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56457432/

相关文章:

java - 为什么注释字符串值没有被实习?

java - 如何创建 POJO?

PHP SimpleXML - xpath 不起作用,但在在线生成器中它可以

java - 如何在Android Studio中安装JDOM

javascript - 在使用 DOM/js/css 时隐藏/显示 div 时出现问题

javascript - contenteditable ="true": creating a syntax highlighted input form

java - 正则表达式模式在没有空格结束时不起作用

java - 尝试使用 GTKLookAndFeel 在 JavaFX 中设置 SwingNode 的样式会卡住应用程序

java - 以编程方式从 Java 导入 XML : items are imported but the returned list is empty

javascript - 无法理解 addEventListener 中的 useCapture 参数