我有以下 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/