嗨,伙计们,我正在对属性进行 xml 解析,我的 xml 是
<CallLists> <CallList ID="1" Name="Name1" Desc="Ignore" CreatedOn="2/15/2011 1:48:30 PM" CreatedBy="def"> <CallList ID="2" Name="Name2" Desc="Agree" CreatedOn="3/8/2011 5:18:52 PM" CreatedBy="abc" > </CallLists>
我想解析这个xml,这样我就可以获得属性值我所做的功课是
在使用肥皂//查看 xml 和遍历从服务器获得响应后
viewXML(Responsedata.toString());
public void viewXML(String xmlStr) throws IOException {
try{
byte[] xmlByteArray=xmlStr.getBytes();
ByteArrayInputStream xmlStream=new ByteArrayInputStream(xmlByteArray);
InputStreamReader xmlReader=new InputStreamReader(xmlStream);
XmlParser parser=new XmlParser(xmlReader);
try{
traverse(parser,"");
}
catch(Exception exc){
exc.printStackTrace();
}
return;
}
catch(IOException e){
return;
}
}
public void traverse(XmlParser parser,String indent) throws Exception{
boolean leave=false;
String sValue="";
do{
ParseEvent event=parser.read();
ParseEvent pe;
switch(event.getType()){
case Xml.START_TAG:
if (event.equals("calllists")){
}
if("ID".equals(event.getAttribute(0))){
pe=parser.read();
sValue=pe.getText()+"~~";
}
if("Name".equals(event.getAttribute(1))){
pe=parser.read();
sValue=sValue+pe.getText()+"~~";
}
if("Desc".equals(event.getAttributes())){
pe=parser.read();
sValue=sValue+pe.getText()+"~~";
}
if("CreatedOn".equals(event.getAttributes())){
pe=parser.read();
sValue=sValue+pe.getText()+"~~";
}
if("CreatedBy".equals(event.getName())){
pe=parser.read();
sValue=sValue+pe.getText()+"~~";
}
traverse(parser,"");
break;
case Xml.END_TAG:
leave=true;
break;
case Xml.END_DOCUMENT:
leave=true;
break;
case Xml.TEXT:
break;
case Xml.WHITESPACE:
break;
default:
}
}while(!leave);
}