xml 파일이 있고 stax xml 파서를 사용하여 <_3-auto> 노드 내부의 텍스트를 가져 오려고했습니다. 텍스트가 노드 내부에 제대로 들어 가지 않아 stax가 값을 가져올 수 없었습니다. 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()
문서에 텍스트 전용 요소 라고 나와 있으므로 사용해서는 안됩니다 .
여기서해야 할 일은 노드 XMLStreamConstants.CHARACTERS
에서 발생 하는 이벤트 도 모니터링 하는 것 <_3-auto>
입니다. 이를 수행하는 간단한 방법은 구문 분석에서 컨텍스트를 처리하여 해당 노드에있을 때를 아는 것입니다. 이 경우 나는 당신이 후이 노드에 있는지 간단한 가정을 제작 <_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);
}
}
그러면 "항공기의 FS755.00에서 후방 동체 지지대 제거"가 인쇄됩니다. 및 "수압 가위 리프트 (1)를 노즈 밸러스트 어셈블리 (2) 아래에 놓습니다." 필터링 할 수있는 공백 문자가 더있는 텍스트입니다.
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다