[英]Read child nodes with same name using StAX
While trying to read XML
file using StAX
I came across this problem. 在尝试使用
StAX
读取XML
文件时,遇到了此问题。
In an XML
file (essentially its an XLIFF
file), I have child nodes with the same name. 在
XML
文件(本质上是XLIFF
文件)中,我有相同名称的子节点。 I couldn't quite figure out how to read these duplicate nodes. 我不太清楚如何读取这些重复的节点。
Below is the part of code that I am trying on, and an example of the XLIFF
file as well 以下是我正在尝试的部分代码,以及
XLIFF
文件的示例
This is only the working part of the code. 这只是代码的工作部分。
Java Code: Java代码:
// Initialize ArrayList to return
ArrayList<SourceCollection> xmlData = new ArrayList<>();
boolean isSource = false;
boolean isTrans = false;
boolean isContext = false;
// Setting Up Data Class
SourceCollection srcData = null;
// Start StAX XLIFF reader
XMLInputFactory xmlInputFactory = XMLInputFactory.newInstance();
try {
XMLStreamReader xmlStreamReader = xmlInputFactory.createXMLStreamReader(inStream);
int event = xmlStreamReader.getEventType();
while (true) {
switch (event) {
case XMLStreamConstants.START_ELEMENT:
switch (xmlStreamReader.getLocalName()) {
case "group":
// Create SourceCollection Object
srcData = new SourceCollection();
srcData.setID(xmlStreamReader.getAttributeValue(0));
break;
case "source":
isSource = true;
break;
case "target":
isTarget = true;
break;
case "context":
isContext = true;
break;
default:
isSource = false;
isTarget = false;
isContext = false;
break;
}
break;
case XMLStreamConstants.CHARACTERS:
if (srcData != null) {
String srcTrns = xmlStreamReader.getText();
if (!Utility.isStringNullOrEmptyOrWhiteSpace(srcTrns)) {
if (isSource) {
srcData.setSource(srcTrns);
isSource = false;
} else if (isTarget) {
srcData.setTarget(srcTrns);
isTarget = false;
}
}
}
break;
case XMLStreamConstants.END_ELEMENT:
if (xmlStreamReader.getLocalName().equals("group")) {
xmlData.add(srcData);
}
break;
}
if (!xmlStreamReader.hasNext()) {
break;
}
event = xmlStreamReader.next();
}
} catch (XMLStreamException ex) {
LOG.log(Level.WARNING, ex.getMessage(), MessageFormat.format("{0} {1}", ex.getCause(), ex.getLocation()));
}
XLIFF file sample: XLIFF文件样本:
<XLIFF>
<xliff version="1.2" xmlns="urn:oasis:names:tc:xliff:document:1.2" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<file datatype="xml">
<body>
<group id="25032014">
<context-group>
<context context-type="sub1">xxxx</context>
<context context-type="sub2">yyyy</context>
<context context-type="sub3"/>
</context-group>
<target-unit>
<source>ABC</source>
<target>ABC</target>
</target-unit>
</group>
</body>
</file>
</xliff>
</XLIFF>
Of course, this is a modified XLIFF file, but structure is exactly the same as original. 当然,这是一个经过修改的XLIFF文件,但是结构与原始文件完全相同。
Any sample or suggestions would be helpful. 任何示例或建议都会有所帮助。
But you already process these duplicates. 但是您已经处理了这些重复项。 I modified your code a little like
我修改了你的代码有点像
switch (event) {
case XMLStreamConstants.START_ELEMENT:
System.out.println(xmlStreamReader.getLocalName());
switch (xmlStreamReader.getLocalName()) {
and the System.out
delivers: 然后
System.out
提供:
XLIFF
xliff
file
body
group
context-group
context
context
context
target-unit
source
target
You see the multiple context
outputs. 您会看到多个
context
输出。 Now you have to adapt your data structure to hold lists of context elements and not only one. 现在,您必须调整数据结构以容纳上下文元素列表,而不仅仅是一个。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.