我试图将xml字符串数据转换为NodeList并迭代..但是,我无法做到。我想迭代nodelist并获取其中的xml数据。这是我的代码
String arrayOfErrorContext = "<item><errorCode>1</errorCode><errorDescription></errorDescription></item>" +
"<item><errorCode>1</errorCode><errorDescription></errorDescription></item>";
if(arrayOfErrorContext!= null && !arrayOfErrorContext.isEmpty()) {
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder;
try
{
builder = factory.newDocumentBuilder();
Document document = builder.parse( new InputSource( new StringReader( arrayOfErrorContext) ) );
NodeList nList1 = document.getElementsByTagName("item");
//iteration Logic on nList1
} catch (Exception e) {
e.printStackTrace();
}
}
上面的代码有什么问题吗?