那三种解析的办法都试过了,android自带的pull dom sax 都解析不了
<string xmlns="http://www.fillaway.com.cn">
<?xml version="1.0" encoding="utf-8"?>
<PDAExchange returnMsg="成功">
<Supplier></Supplier>
<Vid>349</Vid>
<ScanState/>
<Authority>
<Option>1</Option>
<Option>2</Option>
<Option>3</Option>
<Option>4</Option>
</Authority>
</PDAExchange>
</string>
<?xml version="1.0" encoding="utf-8"?>
<PDAExchange returnMsg="成功">
<Supplier></Supplier>
<Vid>349</Vid>
<ScanState/>
<Authority>
<Option>1</Option>
<Option>2</Option>
<Option>3</Option>
<Option>4</Option>
</Authority>
</PDAExchange>
<?xml version="1.0" encoding="utf-8"?> 解析的时候这个必须在第一行
恩 我复制错了
帮忙看看应该怎么写问题出在哪里?
服务器返回的一段XML:
<?xml version="1.0" encoding="utf-8"?>
<string xmlns="http://www.fillaway.com.cn">
<PDAExchange returnMsg="成功">
<Supplier>
</Supplier><Vid>349
</Vid><ScanState/>
<Authority>
<Option>1</Option>
<Option>2</Option>
<Option>3</Option>
<Option>4</Option>
</Authority>
</PDAExchange>
</string>
//这个result装的东西就是上面这段XML
System.err.println("要解析的XML是:"+result);
InputStream inStream= new ByteArrayInputStream(result.getBytes());
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
// 得到一个DocumentBuilder解析类
DocumentBuilder builder = factory.newDocumentBuilder();
// 接收一个xml的字符串来解析xml,Document代表整个xml文档
Document document = builder.parse(inStream);
// 得到xml文档的根元素节点
Element personsElement = document.getDocumentElement();
// 得到标签为PDAExchange的Node对象的集合NodeList
NodeList nodeList = personsElement.getElementsByTagName("string");
for(int i = 0; i < nodeList.getLength(); i++)
{
// 如果该Node是一个Element
if(nodeList.item(i).getNodeType() == Document.ELEMENT_NODE)
{
Element personElement = (Element)nodeList.item(i);
// 得到Node的属性值
String Nodname = personElement.getAttribute("returnMsg");
System.err.println(Nodname);
NodeList childNodesList = personElement.getChildNodes();
for(int j = 0; j < childNodesList.getLength(); j++)
{
if(childNodesList.item(j).getNodeType() == Document.ELEMENT_NODE)
{
// 解析到了person下面的name标签
if("Vid".equals(childNodesList.item(j).getNodeName()))
{
// 得到name标签的文本值
String name = childNodesList.item(j).getFirstChild().getNodeValue();
System.out.println(name);
}
else if("Authority".equals(childNodesList.item(j).getNodeName()))
{
String age = childNodesList.item(j).getFirstChild().getNodeValue();
System.out.println(age);
}
}
}
}
}
@墨者1906:
@林J: // 得到xml文档的根元素节点 这里得到的已经是string 节点
Element personsElement = document.getDocumentElement();
// 得到标签为PDAExchange的Node对象的集合NodeList
NodeList nodeList = personsElement.getElementsByTagName("PDAExchange");
@林J:
谢谢提醒 更改代码后问题还是没解决
@墨者1906: @林J:
System.err.println("要解析的XML是:"+result);
sText.setText("服务器返回的XML: " + result);
//响应的结果为XML 开始解析
DocumentBuilderFactory factory = null;
DocumentBuilder builder = null;
Document document = null;
inputStreamA = null;
factory = DocumentBuilderFactory.newInstance();
inputStreamA = new ByteArrayInputStream(result.getBytes());
factory = DocumentBuilderFactory.newInstance();
// 得到一个DocumentBuilder解析类
builder = factory.newDocumentBuilder();
// 接收一个xml的字符串来解析xml,Document代表整个xml文档
document = builder.parse(inputStreamA);
// 得到xml文档的根元素节点
Element personsElement = document.getDocumentElement();
// 得到标签为PDAExchange的Node对象的集合NodeList
NodeList nodeList = personsElement.getElementsByTagName("PDAExchange");
System.err.print("nodeListl集合的有:"+nodeList.getLength()+"个元素");
for (int i = 0; i < nodeList.getLength(); i++) {
// 如果该Node是一个Element
if (nodeList.item(i).getNodeType() == Document.ELEMENT_NODE) {
Element personElement = (Element) nodeList.item(i);
// 得到Node的属性值
String Nodname = personElement.getAttribute("returnMsg");
System.err.print(Nodname);
NodeList childNodesList = personElement.getChildNodes();
for (int j = 0; j < childNodesList.getLength(); j++) {
if (childNodesList.item(j).getNodeType() == Document.ELEMENT_NODE) {
// 解析到了person下面的name标签
if ("Vid".equals(childNodesList.item(j).getNodeName())) {
// 得到name标签的文本值
String name = childNodesList.item(j).getFirstChild().getNodeValue();
System.err.print(name);
} else if ("Authority".equals(childNodesList.item(j).getNodeName())) {
String age = childNodesList.item(j).getFirstChild().getNodeValue();
System.err.print(age);
}
}
}
}
}
}catch(Exception e){
e.printStackTrace();
}finally {
try {
inputStreamA.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}