从 xml 中获取不在节点内的元素文本
Take element text from xml which is not properly inside node
我有一个 xml 文件,我尝试使用 stax xml 解析器将文本放入 <_3-auto> 节点中。文本不在任何节点内,因此 stax 无法使用 value.Is 以任何其他方式使用 stax 获取值。
<_3-auto>
<prefix>
<autonum>(3)</autonum>
</prefix>
Remove the rear fuselage support from FS755.00 of the aircraft.
</_3-auto>
<_3-auto>
<prefix>
<autonum>(4)</autonum>
</prefix>
Put the hydraulic scissor lift (1) under the nose ballast assembly (2).
</_3-auto>
这是我编写的用于在 _3-auto 节点中获取文本的代码。
try {
XMLInputFactory inputFactory;
inputFactory = XMLInputFactory.newInstance();
InputStream inputStream = new FileInputStream(filePath);
XMLStreamReader streamReader = inputFactory.createXMLStreamReader(inputStream);
while (streamReader.hasNext()) {
int event = streamReader.next();
if (event == XMLStreamConstants.START_ELEMENT) {
if (streamReader.getLocalName().equals("_3-auto")) {
String auto = streamReader.getElementText();
System.out.println(auto);
}
}
}
} catch (Exception e) {
e.printStackTrace();
}
您不应该使用 getElementText()
,因为文档说它适用于 纯文本元素。
您在这里需要做的是在 <_3-auto>
节点发生时还监视 XMLStreamConstants.CHARACTERS
事件。一种简单的方法是在您的解析中处理上下文以了解您何时处于此类节点中。在这种情况下,我做了一个简单的假设,即您在 <_3-auto>
StartElement 事件 之后或 </prefix>
EndElement 事件之后:
boolean current3AutoNode = false;
while (streamReader.hasNext()) {
int event = streamReader.next();
if (event == XMLStreamConstants.START_ELEMENT) {
if (streamReader.getLocalName().equals("_3-auto")) {
current3AutoNode = true;
}
else {
current3AutoNode = false;
}
}
else if (event == XMLStreamConstants.END_ELEMENT) {
if (streamReader.getLocalName().equals("prefix")) {
current3AutoNode = true; // after end of </prefix> we are back in <_3-auto> node
}
else {
current3AutoNode = false;
}
}
if (event == XMLStreamConstants.CHARACTERS && current3AutoNode) {
// these are the characters inside <_3-auto> </_3-auto>
String characters = streamReader.getText();
System.out.println(characters);
}
}
这将打印 "Remove the rear fuselage support from FS755.00 of the aircraft." 和 "Put the hydraulic scissor lift (1) under the nose ballast assembly (2)." 文本,还有一些您可以过滤掉的白色 space 字符。
我有一个 xml 文件,我尝试使用 stax xml 解析器将文本放入 <_3-auto> 节点中。文本不在任何节点内,因此 stax 无法使用 value.Is 以任何其他方式使用 stax 获取值。
<_3-auto>
<prefix>
<autonum>(3)</autonum>
</prefix>
Remove the rear fuselage support from FS755.00 of the aircraft.
</_3-auto>
<_3-auto>
<prefix>
<autonum>(4)</autonum>
</prefix>
Put the hydraulic scissor lift (1) under the nose ballast assembly (2).
</_3-auto>
这是我编写的用于在 _3-auto 节点中获取文本的代码。
try {
XMLInputFactory inputFactory;
inputFactory = XMLInputFactory.newInstance();
InputStream inputStream = new FileInputStream(filePath);
XMLStreamReader streamReader = inputFactory.createXMLStreamReader(inputStream);
while (streamReader.hasNext()) {
int event = streamReader.next();
if (event == XMLStreamConstants.START_ELEMENT) {
if (streamReader.getLocalName().equals("_3-auto")) {
String auto = streamReader.getElementText();
System.out.println(auto);
}
}
}
} catch (Exception e) {
e.printStackTrace();
}
您不应该使用 getElementText()
,因为文档说它适用于 纯文本元素。
您在这里需要做的是在 <_3-auto>
节点发生时还监视 XMLStreamConstants.CHARACTERS
事件。一种简单的方法是在您的解析中处理上下文以了解您何时处于此类节点中。在这种情况下,我做了一个简单的假设,即您在 <_3-auto>
StartElement 事件 之后或 </prefix>
EndElement 事件之后:
boolean current3AutoNode = false;
while (streamReader.hasNext()) {
int event = streamReader.next();
if (event == XMLStreamConstants.START_ELEMENT) {
if (streamReader.getLocalName().equals("_3-auto")) {
current3AutoNode = true;
}
else {
current3AutoNode = false;
}
}
else if (event == XMLStreamConstants.END_ELEMENT) {
if (streamReader.getLocalName().equals("prefix")) {
current3AutoNode = true; // after end of </prefix> we are back in <_3-auto> node
}
else {
current3AutoNode = false;
}
}
if (event == XMLStreamConstants.CHARACTERS && current3AutoNode) {
// these are the characters inside <_3-auto> </_3-auto>
String characters = streamReader.getText();
System.out.println(characters);
}
}
这将打印 "Remove the rear fuselage support from FS755.00 of the aircraft." 和 "Put the hydraulic scissor lift (1) under the nose ballast assembly (2)." 文本,还有一些您可以过滤掉的白色 space 字符。