While trying to read XML
file using StAX
I came across this problem.
In an XML
file (essentially its an XLIFF
file), I have child nodes with the same name. 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
This is only the working part of the code.
Java Code:
// 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 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.
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:
XLIFF
xliff
file
body
group
context-group
context
context
context
target-unit
source
target
You see the multiple context
outputs. Now you have to adapt your data structure to hold lists of context elements and not only one.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.