用dom来解析xml文件
xml文件为:
<?xml version="1.0" encoding="UTF-8"?>
<mobile-list>
<mobile type="Nokia2652">
<wap2>false</wap2>
<width>115</width>
</mobile>
<mobile type="Nokia2650">
<wap2>false</wap2>
<width>115</width>
</mobile>
<mobile type="Nokia6108">
<wap2>false</wap2>
<width>115</width>
</mobile>
</mobile-list>
用DOM来解析类:
package com.pk.xml;
import java.io.File;
import java.io.IOException;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;
public class DOMxml {
public static void main(String[] args) throws ParserConfigurationException, SAXException, IOException {
//获取dom工厂
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
//获取dom解析器
DocumentBuilder builder = factory.newDocumentBuilder();
//解析的文件
File file = new File("D:\\项目\\me\\mobilelist.xml");
Document document = builder.parse(file);
//获取根节点
Element root = document.getDocumentElement();
//获取子节点列表
NodeList books = root.getChildNodes();
for(int i= 0 ; i<books.getLength();i++){
//获取每一个子节点
Node book = books.item(i);
if(book.getNodeType()==Node.ELEMENT_NODE){
//获取属性的值
String type = book.getAttributes().getNamedItem("type").getNodeValue();
System.out.print(type+"\t");
//循环子节点
for(Node node=book.getFirstChild();node!=null;node=node.getNextSibling()){
if(node.getNodeType()==Node.ELEMENT_NODE){
if(node.getNodeName().equals("wap2")){
String wap2 = node.getFirstChild
相关文档:
什么是 XML?
可扩展标记语言 (XML) 是 Web 上的数据通用语言。它使开发人员能够将结构化数据,从许多不同的应用程序传递到桌面,进行本地计算和演示。XML 允许为特定应用程序创建唯一的数据格式。它还是在服务器之间传输结构化数据的理想格式。
什么是 MSXML?
MSXML 是提供核心 XML 服务的 Microsoft 软 ......
四种XML解析方法
xml文件:
<?xml version="1.0" encoding="GB2312"?>
<RESULT>
<VALUE>
<NO>A1234</NO>
<ADDR>四川省XX县XX镇XX路X段XX号</ADDR>
</VALUE>
<VALUE>
<NO>B1234</NO>
<ADDR>四川省XX市XX乡XX村XX组</ADDR>
</VALUE>
</RESULT>
1)DOM
& ......
今天的项目模块中用到根据数据库里的资料生成xml文件,主要步骤如下:
(1) 连接数据库,取出数据;
(2) 创建结点,添加子项;
(3) 写入文件“test.xml”中;
具体代码如下:
import java.io.FileOutputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.Result ......
wsdl.xml文件
<?xml version="1.0" encoding="UTF-8" ?>
<definitions name="MobilePhoneService"
targetNamespace="www.mobilephoneservice.com/MobilePhoneService-interface"
xmlns="http://schemas.xmlsoap.org/wsdl/"
  ......
声明
/// <summary>
/// XML文档
/// </summary>
XmlDocument xmldoc;
&n ......