解析xml的字符串

我获得到一个xml的字符串现在要从中取出相应信息请问有什么方便快捷的方法吗  最好能给出例子    
String  xmlstring=  
<?xml  version="1.0"  encoding="UTF8"?>    
<login_info  code="1"  tsession="232133213231">    
 <user_info>    
   <mobile>1</mobile>    
   <login_name>abc</login_name>  
   <nick_name>aaaa</nick_name>  
 </user_info>  
</login_info>    
 
我要取出  "abc"  存入  loginname  和  "aaaa"  存入  nickname  
即:    loginname="abc"        nickname=  "aaaa"  
希望给出详细步骤    谢谢!  
---------------------------------------------------------------  
 
DocumentBuilderFactory  dbf  =  DocumentBuilderFactory.newInstance();  
               DocumentBuilder  db  =  null;  
               try  {  
                       db  =  dbf.newDocumentBuilder();  
               }  catch  (ParserConfigurationException  pce)  {  
                       pce.printStackTrace();  
               }  
 
               Document  doc  =  null;  
               try  {  
                       doc  =  db.parse(xmlstring);//  xml地址  
               }  catch  (Exception  e)  {  
                       e.printStackTrace();  
               }    
Element  root  =  doc.getDocumentElement();  
NodeList  nl  =  root.getChildNodes();  
                       for  (int  i  =  0;  i  <  nl.getLength();  i++)  {  
                                   Node  aNode  =  nl.item(i);  
                                   if  (aNode.getNodeType()  ==  Node.ELEMENT_NODE)  {  
                                               if  (aNode.getNodeName().equals("user_info"))  {  
NodeList  gnl  =  aNode.getChildNodes();  
                                                         
                                                       for  (int  j  =  0;  j  <  gnl.getLength();  j++)  {  
                                                                       Node  gNode  =  gnl.item(j);  
                                                                       if  (gNode.getNodeType()  ==  Node.ELEMENT_NODE)  {                                                                                  
                                                                                   if  (gNode  
                                                                                               .getNodeName()  
                                                                                               .equals("login_name"))  {  
 
String  userName=getElementText((Element)gNode);//保存userName  
}  
else  if  (gNode  
                                                                                               .getNodeName()  
                                                                                               .equals("nick_name"))  {  
 
String  nickName=getElementText((Element)gNode);//保存userName  
}  
 
}  
}  
}  
}  
///  }和个数你自己调整一下,另一个你照着写一下吧  
 
 
private  String  getElementText(Element  e)  {  
                       NodeList  nl  =  e.getChildNodes();  
                       for  (int  i  =  0;  i  <  nl.getLength();  i++)  {  
                                   Node  n  =  nl.item(i);  
                                   if  (n  instanceof  org.w3c.dom.Text)  {  
                                               return  n.getNodeValue();                                                  
                                   }  
                       }  
                       return  "";  
           }

摘自http://community.csdn.net/Expert/TopicView3.asp?id=4197716
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值