java 解析xml格式的字符串

网上有好多种解析xml格式的字符串方式,在此记录下自己用的一种:


DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();

DocumentBuilder builder = factory.newDocumentBuilder();  

Document doc = builder.parse(new InputSource(new StringReader(result)));//result就是需要解析的xml字符串 
 
NodeList nl = doc.getElementsByTagName("returnsms"); //总节点

String sendRes = doc.getElementsByTagName("returnstatus").item(0).getFirstChild().getNodeValue();//获取returnstatus节点里的值

猜你喜欢

转载自zisetoufa.iteye.com/blog/2315137