XML文件:

response:

  1. <?xml version="1.0"?>
  2. <soap:Envelope
  3. xmlns:soap="http://www.w3.org/2001/12/soap-envelope" soap:encodingStyle="http://www.w3.org/2001/12/soap-encoding">
  4. <soap:Body xmlns:m="http://www.nwpu.edu.cn/soa/xml/test ">
  5. <m:GetWeatherResponse>
  6. <m:Temperature>13.2</m:Temperature>
  7. <m:Weather >sunny</m:Weather >
  8. </m:GetWeatherResponse>
  9. </soap:Body>
  10. </soap:Envelope>

request:

  1. <?xml version="1.0"?>
  2. <soap:Envelope xmlns:soap="http://www.w3.org/2001/12/soap-envelope" soap:encodingStyle="http://www.w3.org/2001/12/soap-encoding">
  3. <soap:Body xmlns:n="http://www.nwpu.edu.cn/soa/xml/test">
  4. <n:GetWeather>
  5. <n:CityName>西安</n:CityName>
  6. </n:GetWeather>
  7. </soap:Body>
  8. </soap:Envelope>

解析函数:

  1. package com.wjy.marshal;
  2. import java.io.File;
  3.  
  4. import javax.xml.parsers.DocumentBuilder;
  5. import javax.xml.parsers.DocumentBuilderFactory;
  6.  
  7. import org.w3c.dom.Document;
  8. import org.w3c.dom.Element;
  9. import org.w3c.dom.NodeList;
  10.  
  11. public class GetCityName
  12. {
  13. private String xmlFilePath="C://Documents and Settings/Administrator/桌面/request.xml";
  14. public String getCityName()
  15. {
  16. String result = "";
  17. try {
  18. // step 1: 获得dom解析器工厂(工作的作用是用于创建具体的解析器)
  19. DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
  20. //System.out.println("class name: " + dbf.getClass().getName());
  21. // step 2:获得具体的dom解析器
  22. DocumentBuilder db = dbf.newDocumentBuilder();
  23. //System.out.println("class name: " + db.getClass().getName());
  24. // step3: 解析一个xml文档,获得Document对象(根结点)
  25. Document document = db.parse(new File(xmlFilePath));
  26. NodeList nodeList=document.getElementsByTagName("n:GetWeather");
  27. Element element=(Element)nodeList.item();
  28. result=element.getElementsByTagName("n:CityName").item().getFirstChild().getNodeValue();
  29.  
  30. } catch (Exception e) {
  31. // TODO: handle exception
  32. }
  33.  
  34. return result;
  35. }
  36. }
  1. package com.wjy.marshal;
  2. import java.io.File;
  3.  
  4. import javax.xml.parsers.DocumentBuilder;
  5. import javax.xml.parsers.DocumentBuilderFactory;
  6.  
  7. import org.w3c.dom.Document;
  8. import org.w3c.dom.Element;
  9. import org.w3c.dom.NodeList;
  10.  
  11. public class GetCityWeather
  12. {
  13. private String xmlFilePath="C://Documents and Settings/Administrator/桌面/response.xml";
  14. public String getCityWeather()
  15. {
  16. String tempurature = "";
  17. String weather="";
  18. try {
  19. // step 1: 获得dom解析器工厂(工作的作用是用于创建具体的解析器)
  20. DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
  21. //System.out.println("class name: " + dbf.getClass().getName());
  22. // step 2:获得具体的dom解析器
  23. DocumentBuilder db = dbf.newDocumentBuilder();
  24. //System.out.println("class name: " + db.getClass().getName());
  25. // step3: 解析一个xml文档,获得Document对象(根结点)
  26. Document document = db.parse(new File(xmlFilePath));
  27. NodeList nodeList=document.getElementsByTagName("m:GetWeatherResponse");
  28. Element element=(Element)nodeList.item();
  29. tempurature=element.getElementsByTagName("m:Temperature").item().getFirstChild().getNodeValue();
  30. weather=element.getElementsByTagName("m:Weather").item().getFirstChild().getNodeValue();
  31.  
  32. System.out.println(tempurature+" "+weather);
  33. } catch (Exception e) {
  34. // TODO: handle exception
  35. }
  36.  
  37. return tempurature;
  38. }
  39. }

主函数:

  1. import com.wjy.marshal.GetCityName;
  2. import com.wjy.marshal.GetCityWeather;
  3.  
  4. public class zhu {
  5. public static void main(String args[]){
  6. GetCityWeather getCityWeather=new GetCityWeather();
  7. getCityWeather.getCityWeather();
  8.  
  9. GetCityName getCityName=new GetCityName();
  10. System.out.println(getCityName.getCityName());
  11. }
  12. }

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. 深入学习微框架:Spring Boot

    Spring Boot是由Pivotal团队提供的全新框架,其设计目的是用来简化新Spring应用的初始搭建以及开发过程.该框架使用了特定的方式来进行配置,从而使开发人员不再需要定义样板化的配置.通过 ...

  2. 【状态DP】 HDU 1074 Doing Homework

    原题直通车:HDU  1074  Doing Homework 题意:有n门功课需要完成,每一门功课都有时间期限t.完成需要的时间d,如果完成的时间走出时间限制,就会被减 (d-t)个学分.问:按怎样 ...

  3. 什么是防盗链设置中的空Referer

    设置防盗链时候指明和不指明空Referer的差别及实现后的效果? 什么是Referer? 这里的 Referer 指的是HTTP头部的一个字段,也称为HTTP来源地址(HTTP Referer).用来 ...

  4. linux shell编程指南第十八章------控制流结构

    在书写正确脚本前,大概讲一下退出状态.任何命令进行时都将返回一个退出状态.如 果要观察其退出状态,使用最后状态命令: $ echo $? 主要有4种退出状态.前面已经讲到了两种,即最后命令退出状态$ ...

  5. 百度地图new BMap.LocalCity() 问题

    <html> <head> <meta http-equiv="Content-Type" content="text/html; char ...

  6. c++ try throw catch

    c++ try throw catch 这三者联合使用 , try { statement list; } catch( typeA arg ) { statement list; } catch( ...

  7. objective-c 中数据类型之中的一个 几何数据类型(CGPoint,CGSize,CGRect)

    // CGPoint 结构体数据原型, 用于声明一个点: /* Points. */ struct CGPoint { CGFloat x; CGFloat y; }; typedef struct ...

  8. Android Fragement学习笔记(三)----PreferenceFragment的使用

    相信大家对Perference都比較熟悉了,也就是我们常说的偏好设置,首选项设置,能够保存一些数据,比如我们在上一次使用的时候的一些内容,希望在下一次启动后依旧生效,而不须要再进行配置那么麻烦.一般这 ...

  9. LeetCode——Populating Next Right Pointers in Each Node II

    Follow up for problem "Populating Next Right Pointers in Each Node". What if the given tre ...

  10. linux内核基础(系统调用,简明)

    内核基础(系统调用) 在说系统调用之前.先来说说内核是怎么和我们交互的.或者说是怎么和我们产生交集的. 首先,内核是用来控制硬件的仅仅有内核才干直接控制硬件,所以说内核非常重要,假设内核被控制那么电脑 ...