こんにちは、私は私の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 を解析したい
SOAP //xml の表示とトラバースを使用して SERVER から応答を取得した後
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);
}