gpt4 book ai didi

java - XMLStreamReader 如何使用相同类型的嵌套元素

转载 作者:行者123 更新时间:2023-12-02 04:28:41 27 4
gpt4 key购买 nike

我正在使用 XMLStreamReader 并解析以下 XML:

<root>
<element>
<attribute>level0</attribute>
<element>
<attribute>level1</attribute>
<element>
<attribute>level2</attribute>
</element>
</element>
</element>
</root>

我正在构建我的 XMLStreamReader:

XMLStreamReader reader = XMLInputFactory.newInstance().createXMLStreamReader(
new ByteArrayInputStream(document.getBytes()));

不幸的是,当我使用 reader.next(); 到达第一个结束元素标记时,出现以下异常:

javax.xml.stream.XMLStreamException: ParseError at [row,col]:[7,14]
Message: XML document structures must start and end within the same entity.

有没有办法覆盖 XMLStreamReader 的默认行为来解决这个问题?

编辑

这是我正在使用的代码:

@Override
protected void map(LongWritable key, Text value, Mapper<LongWritable, Text, Text, Text>.Context context)
throws IOException, InterruptedException {
String document = value.toString();
System.out.println("'" + document + "'");
try {
XMLStreamReader reader = XMLInputFactory.newInstance().createXMLStreamReader(
new ByteArrayInputStream(document.getBytes()));
String propertyName = "";
String propertyValue = "";
String currentElement = "";
while (reader.hasNext()) {
int code = reader.next();
switch (code) {
case START_ELEMENT:
currentElement = reader.getLocalName();
break;
case CHARACTERS:
if (currentElement.equalsIgnoreCase("element")) {
propertyName += reader.getText();
} else if (currentElement.equalsIgnoreCase("attribute")) {
propertyValue += reader.getText();
}
break;
}
}
reader.close();
context.write(new Text(propertyName.trim()), new Text(propertyValue.trim()));
} catch (Exception e) {
e.printStackTrace();
}
}

最佳答案

示例 XML 文档和/或 StAX 解析器没有任何问题,可以使用以下代码进行检查:

@Test
public void testSO_31815379() throws XMLStreamException, UnsupportedEncodingException {
final String xml =
"<root>\n" +
" <element>\n" +
" <attribute>level0</attribute>\n" +
" <element>\n" +
" <attribute>level1</attribute>\n" +
" <element>\n" +
" <attribute>level2</attribute>\n" +
" </element>\n" +
" </element>\n" +
" </element>\n" +
"</root>";

final XMLStreamReader reader = XMLInputFactory.newInstance()
.createXMLStreamReader(new ByteArrayInputStream(xml.getBytes("UTF-8")));
LOG.info("Using XMLStreamReader implementation: %s", reader.getClass().getName());

reader.require(XMLStreamConstants.START_DOCUMENT, null, null);
int event;
while ((event = reader.next()) != XMLStreamConstants.END_DOCUMENT) {
LOG.info(StaxUtils.eventDescription(reader));
}
reader.require(XMLStreamConstants.END_DOCUMENT, null, null);
reader.close();
}

输出(StaxUtils.eventDescription 是自定义帮助器方法)

Using XMLStreamReader implementation: com.sun.org.apache.xerces.internal.impl.XMLStreamReaderImpl
START_ELEMENT<{}root>
CHARACTERS=<whitespace>
START_ELEMENT<{}element>
CHARACTERS=<whitespace>
START_ELEMENT<{}attribute>
CHARACTERS='level0'
END_ELEMENT<attribute>
CHARACTERS=<whitespace>
START_ELEMENT<{}element>
CHARACTERS=<whitespace>
START_ELEMENT<{}attribute>
CHARACTERS='level1'
END_ELEMENT<attribute>
CHARACTERS=<whitespace>
START_ELEMENT<{}element>
CHARACTERS=<whitespace>
START_ELEMENT<{}attribute>
CHARACTERS='level2'
END_ELEMENT<attribute>
CHARACTERS=<whitespace>
END_ELEMENT<element>
CHARACTERS=<whitespace>
END_ELEMENT<element>
CHARACTERS=<whitespace>
END_ELEMENT<element>
CHARACTERS=<whitespace>
END_ELEMENT<root>

关于java - XMLStreamReader 如何使用相同类型的嵌套元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31815379/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com