使用StAX读取XML字符串

我第一次使用stax解析XML字符串。我找到了一些示例,但无法使我的代码正常工作。这是我的代码的最新版本:

public class AddressResponseParser

{

private static final String STATUS = "status";

private static final String ADDRESS_ID = "address_id";

private static final String CIVIC_ADDRESS = "civic_address";

String status = null;

String addressId = null;

String civicAddress = null;

public static AddressResponse parseAddressResponse(String response)

{

try

{

byte[] byteArray = response.getBytes("UTF-8");

ByteArrayInputStream inputStream = new ByteArrayInputStream(byteArray);

XMLInputFactory inputFactory = XMLInputFactory.newInstance();

XMLStreamReader reader = inputFactory.createXMLStreamReader(inputStream);

while (reader.hasNext())

{

int event = reader.next();

if (event == XMLStreamConstants.START_ELEMENT)

{

String element = reader.getLocalName();

if (element.equals(STATUS))

{

status = reader.getElementText();

continue;

}

if (element.equals(ADDRESS_ID))

{

addressId = reader.getText();

continue;

}

if (element.equals(CIVIC_ADDRESS))

{

civicAddress = reader.getText();

continue;

}

}

}

}

catch (Exception e)

{

log.error("Couldn't parse AddressResponse", e);

}

}

}

我将手表放在“事件”和“ reader.getElementText()”上。当代码停止时

String element = reader.getLocalName();

将显示“ reader.getElementText()”值,但是一旦它偏离该行,就无法评估。当代码停止时:

status = reader.getElementText();

“元素”手表显示正确的值。最后,当我又将代码步进一行时,我捕获了此异常:

(com.ctc.wstx.exc.WstxParsingException) com.ctc.wstx.exc.WstxParsingException: Current state not START_ELEMENT

at [row,col {unknown-source}]: [1,29]

我尝试使用status = reader.getText();代替,但随后出现此异常:

(java.lang.IllegalStateException) java.lang.IllegalStateException: Not a textual event (END_ELEMENT)

有人可以指出我在做什么错吗?

编辑:

添加用于测试的JUnit代码:

public class AddressResponseParserTest

{

private String status = "OK";

private String address_id = "123456";

private String civic_address = "727";

@Test

public void testAddressResponseParser() throws UnsupportedEncodingException, XMLStreamException

{

AddressResponse parsedResponse = AddressResponseParser.parseAddressResponse(this.responseXML());

assertEquals(this.status, parsedResponse.getStatus());

assertEquals(this.address_id, parsedResponse.getAddress()

.getAddressId());

assertEquals(this.civic_address, parsedResponse.getAddress()

.getCivicAddress());

}

private String responseXML()

{

StringBuffer buffer = new StringBuffer();

buffer.append("<response>");

buffer.append("<status>OK</status>");

buffer.append("<address>");

buffer.append("<address_id>123456</address_id>");

buffer.append("<civic_address>727</civic_address>");

buffer.append("</address>");

buffer.append("</response>");

return buffer.toString();

}

}

回答:

我找到了一个使用XMLEventReader而不是XMLStreamReader的解决方案:

public MyObject parseXML(String xml)

throws XMLStreamException, UnsupportedEncodingException

{

byte[] byteArray = xml.getBytes("UTF-8");

ByteArrayInputStream inputStream = new ByteArrayInputStream(byteArray);

XMLInputFactory inputFactory = XMLInputFactory.newInstance();

XMLEventReader reader = inputFactory.createXMLEventReader(inputStream);

MyObject object = new MyObject();

while (reader.hasNext())

{

XMLEvent event = (XMLEvent) reader.next();

if (event.isStartElement())

{

StartElement element = event.asStartElement();

if (element.getName().getLocalPart().equals("ElementOne"))

{

event = (XMLEvent) reader.next();

if (event.isCharacters())

{

String elementOne = event.asCharacters().getData();

object.setElementOne(elementOne);

}

continue;

}

if (element.getName().getLocalPart().equals("ElementTwo"))

{

event = (XMLEvent) reader.next();

if (event.isCharacters())

{

String elementTwo = event.asCharacters().getData();

object.setElementTwo(elementTwo);

}

continue;

}

}

}

return object;

}

我仍然会对看到使用XMLStreamReader的解决方案感兴趣。

以上是 使用StAX读取XML字符串 的全部内容, 来源链接: utcz.com/qa/403891.html

回到顶部