DOM解析XML文件实例

时间:2024-01-14 19:44:20

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();
result=element.getElementsByTagName("n:CityName").item().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();
tempurature=element.getElementsByTagName("m:Temperature").item().getFirstChild().getNodeValue();
weather=element.getElementsByTagName("m:Weather").item().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());
}
}