DOM解析XML文件实例
XML文件:
response:
<?xml version="1.0"?> <soap:Envelope xmlns:soap="http://www.w3.org/2001/12/soap-envelope" soap:encodingStyle="http://www.w3.org/2001/12/soap-encoding"> <soap:Body xmlns:m="http://www.nwpu.edu.cn/soa/xml/test "> <m:GetWeatherResponse> <m:Temperature>13.2</m:Temperature> <m:Weather >sunny</m:Weather > </m:GetWeatherResponse> </soap:Body> </soap:Envelope>
request:
<?xml version="1.0"?> <soap:Envelope xmlns:soap="http://www.w3.org/2001/12/soap-envelope" soap:encodingStyle="http://www.w3.org/2001/12/soap-encoding"> <soap:Body xmlns:n="http://www.nwpu.edu.cn/soa/xml/test"> <n:GetWeather> <n:CityName>西安</n:CityName> </n:GetWeather> </soap:Body> </soap:Envelope>
解析函数:
package com.wjy.marshal; import java.io.File; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; import org.w3c.dom.Document; import org.w3c.dom.Element; import org.w3c.dom.NodeList; public class GetCityName { private String xmlFilePath="C://Documents and Settings/Administrator/桌面/request.xml"; public String getCityName() { String result = ""; try { // step 1: 获得dom解析器工厂(工作的作用是用于创建具体的解析器) DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); //System.out.println("class name: " + dbf.getClass().getName()); // step 2:获得具体的dom解析器 DocumentBuilder db = dbf.newDocumentBuilder(); //System.out.println("class name: " + db.getClass().getName()); // step3: 解析一个xml文档,获得Document对象(根结点) Document document = db.parse(new File(xmlFilePath)); NodeList nodeList=document.getElementsByTagName("n:GetWeather"); Element element=(Element)nodeList.item(0); result=element.getElementsByTagName("n:CityName").item(0).getFirstChild().getNodeValue(); } catch (Exception e) { // TODO: handle exception } return result; } }
package com.wjy.marshal; import java.io.File; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; import org.w3c.dom.Document; import org.w3c.dom.Element; import org.w3c.dom.NodeList; public class GetCityWeather { private String xmlFilePath="C://Documents and Settings/Administrator/桌面/response.xml"; public String getCityWeather() { String tempurature = ""; String weather=""; try { // step 1: 获得dom解析器工厂(工作的作用是用于创建具体的解析器) DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); //System.out.println("class name: " + dbf.getClass().getName()); // step 2:获得具体的dom解析器 DocumentBuilder db = dbf.newDocumentBuilder(); //System.out.println("class name: " + db.getClass().getName()); // step3: 解析一个xml文档,获得Document对象(根结点) Document document = db.parse(new File(xmlFilePath)); NodeList nodeList=document.getElementsByTagName("m:GetWeatherResponse"); Element element=(Element)nodeList.item(0); tempurature=element.getElementsByTagName("m:Temperature").item(0).getFirstChild().getNodeValue(); weather=element.getElementsByTagName("m:Weather").item(0).getFirstChild().getNodeValue(); System.out.println(tempurature+" "+weather); } catch (Exception e) { // TODO: handle exception } return tempurature; } }
主函数:
import com.wjy.marshal.GetCityName; import com.wjy.marshal.GetCityWeather; public class zhu { public static void main(String args[]){ GetCityWeather getCityWeather=new GetCityWeather(); getCityWeather.getCityWeather(); GetCityName getCityName=new GetCityName(); System.out.println(getCityName.getCityName()); } }
相关推荐
Yakamoz 2020-05-26
与卿画眉共浮生 2020-10-14
xiyang 2020-08-21
XGQ 2020-07-04
Andrewjdw 2020-05-29
行吟阁 2020-05-18
88491874 2020-04-30
baijinswpu 2020-07-29
leonranri 2020-07-26
zhongliwen 2020-07-05
麋鹿麋鹿迷了路 2020-07-05
zengyu00 2020-07-05
CoderBoy 2020-06-28
whbing 2020-06-28
绝望的乐园 2020-06-27
wellfly 2020-06-26
菇星獨行 2020-06-25
草原孤狼 2020-06-25
坚持着执着 2020-06-16