我想使用STAX Iterator API解析xml节点并获取每个id节点的值。在下面的代码中,如何获取id type = id2或id3的对应值。我该怎么办?
<entity>
<id type="id1">8500123</id>
<id type="id2">8500124</id>
<id type="id3">8500125</id>
<link idType="someId">99369</link>
</entity>
下面的STAX Iterator API代码;
XMLEventReader xmlEventReader = xmlInputFactory.createXMLEventReader(new FileInputStream(fileName));
while (xmlEventReader.hasNext()) {
XMLEvent xmlEvent = xmlEventReader.nextEvent();
if (xmlEvent.isStartElement()) {
StartElement startElement = xmlEvent.asStartElement();
if (startElement.getName().getLocalPart().equals("entity")) {
XMLEvent xmlEvent2 = xmlEventReader.nextEvent();//has to forgo this bcoz it always return a new line.
XMLEvent xmlEvent3 = xmlEventReader.nextEvent();
if (xmlEvent3.isStartElement()) {
StartElement startElement2 = xmlEvent3.asStartElement();
if (startElement2.getName().getLocalPart().equals("id")) {
connector = new Connector();
Attribute idAttr = startElement2.getAttributeByName(new QName("type"));
if(idAttr.getName().equals("id1")){
connector.setId1(idAttr.getValue());
}
}
}
}
}
}
答案 0 :(得分:0)
由于问题很老,可能不再是问题,但是我只是想做同样的事情。样本代码几乎在那儿了。缺少的步骤是检查XMLStreamConstants.CHARACTERS
的事件类型,该事件类型对应于以下任一情况:
因此,在您的情况下,仅在满足所有这些条件时才要提取数据:
XMLStreamConstants.CHARACTERS
(在这种情况下,EventType.isCharacters()
返回true)。 XMLStreamConstants.START_ELEMENT
。可以通过调整现有代码来做到这一点,但是更干净,更通用的方法是使用XMLEventReader
语句迭代处理case
返回的事件。要获取开始标记和结束标记之间的数据值:
Characters characters = xmlEvent.asCharacters();
String data = characters.getData();
这是一个工作示例,其中文件 sample.xml 包含OP中的数据:
package pkg;
import java.io.FileReader;
import java.io.IOException;
import java.io.Reader;
import javax.xml.namespace.QName;
import javax.xml.stream.XMLEventReader;
import javax.xml.stream.XMLInputFactory;
import javax.xml.stream.XMLStreamConstants;
import javax.xml.stream.XMLStreamException;
import javax.xml.stream.events.Attribute;
import javax.xml.stream.events.Characters;
import javax.xml.stream.events.EndElement;
import javax.xml.stream.events.StartElement;
import javax.xml.stream.events.XMLEvent;
public class StaxDemo {
public static void main(String[] args) throws XMLStreamException, IOException {
try (Reader reader = new FileReader("sample.xml");) {
XMLInputFactory xmlInputFactory = XMLInputFactory.newFactory();
XMLEventReader xmlEventReader = xmlInputFactory.createXMLEventReader(reader);
parseXml(xmlEventReader);
}
}
static void parseXml(XMLEventReader xmlEventReader) throws XMLStreamException {
String typeValue = null;
while (xmlEventReader.hasNext()) {
XMLEvent xmlEvent = xmlEventReader.nextEvent();
switch (xmlEvent.getEventType()) {
case XMLStreamConstants.START_DOCUMENT:
System.out.println("XMLEvent.START_DOCUMENT");
break;
case XMLStreamConstants.START_ELEMENT:
StartElement startElement = xmlEvent.asStartElement();
Attribute typeAttribute = startElement.getAttributeByName(new QName("type"));
if (typeAttribute != null) {
typeValue = typeAttribute.getValue();
}
System.out.println("XMLEvent.START_ELEMENT: <" + startElement.getName() + "> " + "type=" + typeValue);
break;
case XMLStreamConstants.CHARACTERS:
Characters characters = xmlEvent.asCharacters();
if ((typeValue != null)) { // Non-null if preceding event was for START_ELEMENT.
if ((typeValue.equals("id2")) || (typeValue.equals("id3"))) {
String data = characters.getData();
System.out.println("XMLEvent.CHARACTERS: data=[" + data + "]");
}
typeValue = null;
}
break;
case XMLStreamConstants.END_ELEMENT:
EndElement endElement = xmlEvent.asEndElement();
System.out.println("XMLEvent.END_ELEMENT: </" + endElement.getName() + ">");
break;
case XMLStreamConstants.END_DOCUMENT:
System.out.println("XMLEvent.END_DOCUMENT");
break;
default:
System.out.println("case default: Event Type = " + xmlEvent.getEventType());
break;
}
}
}
}
我添加了几个println()
调用,只是为了阐明XMLEventReader
对文件的处理方式。输出如下:
XMLEvent.START_DOCUMENT
XMLEvent.START_ELEMENT: <entity> type=null
XMLEvent.START_ELEMENT: <id> type=id1
XMLEvent.END_ELEMENT: </id>
XMLEvent.START_ELEMENT: <id> type=id2
XMLEvent.CHARACTERS: data=[z8500124]
XMLEvent.END_ELEMENT: </id>
XMLEvent.START_ELEMENT: <id> type=id3
XMLEvent.CHARACTERS: data=[z8500125]
XMLEvent.END_ELEMENT: </id>
XMLEvent.START_ELEMENT: <link> type=null
XMLEvent.END_ELEMENT: </link>
XMLEvent.END_ELEMENT: </entity>
XMLEvent.END_DOCUMENT
Oracle提供了a tutorial for StAX。当所有基本信息都存在时,我发现它有点混乱。