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());
}
}

DOM解析XML文件实例的更多相关文章

  1. 使用DOM解析xml文件

    使用DOM解析xml文件 要解析的xml文件如下: <?xml version="1.0" encoding="UTF-8"?> <Langu ...

  2. DOM解析XML文件例子

    DOM解析XML文件是一次性将目标文件中的所有节点都读入,然后再进行后续操作的方式. 一般分为以下几步: 1. 定义好目标XML文件路径path . 2. 实例化DOM解析工厂对象 ,Document ...

  3. JAVA中使用DOM解析XML文件

    XML是一种方便快捷高效的数据保存传输的格式,在JSON广泛使用之前,XML是服务器和客户端之间数据传输的主要方式.因此,需要使用各种方式,解析服务器传送过来的信息,以供使用者查看. JAVA作为一种 ...

  4. 使用DOM解析XML文件,、读取xml文件、保存xml、增加节点、修改节点属性、删除节点

    使用的xml文件 <?xml version="1.0" encoding="GB2312" ?> <PhoneInfo> <Br ...

  5. SAX方式解析XML文件实例

    books.XML文件: 书籍book.java实体类: public class Book { private String id; private String name; private Str ...

  6. DOM方式解析XML文件实例

    books.XML文件: <?xml version="1.0" encoding="utf-8"?><bookstore>    &l ...

  7. Qt中使用DOM解析XML文件或者字符串二(实例)

    介绍 在Qt中提供了QtXml模块实现了对XML数据的处理,我们在Qt帮助中输入关键字QtXml Module,可以看到该模块的类表.在这里我们可以看到所有相关的类,它们主要是服务于两种操作XML文档 ...

  8. Qt中使用DOM解析XML文件或者字符串(实例)

    因为需要读取配置文件,我的配置文件采用xml:因此编写了使用qt读取xml文件内容的代码,xml文件如下: <?xml version="1.0" encoding=&quo ...

  9. Dom解析XML文件具体用法

    public class Dom4j { public static void main(String[] args) throws Exception { List<Student> l ...

随机推荐

  1. 谷歌浏览器訪问不了啦,oh shit!

    从这个礼拜開始,一直訪问不了谷歌.该死的,百度找的资料全然是牛头不正确马嘴 我也问了其它人.有的人也是打不开,蛋疼的 可是如今找到几种方法了,还真管用 第一种方法:加上ncr http://www.g ...

  2. gbs remotebuild使用说明

    本文件从:https://source.tizen.org/documentation/articles/gbs-remotebuild翻译而来. 1 远程构建 使用remotebuild子指令将本地 ...

  3. <转载>CSS解决图片过大撑破DIV的方法

    DIV+CSS网页内容中如果插入大于DIV层宽度显示,过大的图片将会撑破网页宽度显示从而网页严重变形,您是否遇到过?这里DIVCSS5给大家介绍几种解决图片撑破撑开网页DIV层方法. 图片撑破宽度解决 ...

  4. 【ASP.NET Web API教程】2.3.6 创建产品和订单控制器

    原文:[ASP.NET Web API教程]2.3.6 创建产品和订单控制器 注:本文是[ASP.NET Web API系列教程]的一部分,如果您是第一次看本博客文章,请先看前面的内容. Part 6 ...

  5. 基于visual Studio2013解决面试题之0402合并升序链表并去重

     题目

  6. 关于yaf的控制器命名,一个纠结的问题(续)

    以下方案缺少loader相关的步骤,明天补上!!! 前面写过一篇<关于yaf的控制器命名,一个纠结的问题>.没想到yaf群里面也有跟我遇到一样问题的人,分享下解决办法. 写完那篇博文后,我 ...

  7. Windows 编程之 对话框总结

    关于对话框 1 对话框种类 对话框也是一种资源,Windows中对话框分为模态的和非模态的,以及Windows系统中已经定义好的那些通用对话框,比方打开文件,目录,调色板,字符串查找等. 2 模态对话 ...

  8. 四个机器学习一步一步入门约束波尔兹曼机RBM

  9. vs2008中使用gdi+的设置

    vs2008中使用gdi+ 1.新建一个mfc工程 2.在stdafx.h文件中加入以下几行语句: #include <gdiplus.h>                //#pragm ...

  10. MFC 在对话框显示图片的多种方法

      我们先从简单的开始吧.先分一个类: (一) 非动态显示图片(即图片先通过资源管理器载入,有一个固定ID) (二) 动态载入图片(即只需要在程序中指定图片的路径即可载入) 为方便说明,我们已经建好一 ...