用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文件:
<?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
& ......
/*
练习使用java.util.properties类包来操作propertes及XML文件,通过store方法的调用可实现xml/properties文件的相互保存转化
*/
import java.util.*;
import java.io.*;
public class TestPropertes
{
public static void main(String[] args) {
Properties pp = new Properties();
Fi ......
声明
/// <summary>
/// XML文档
/// </summary>
XmlDocument xmldoc;
&n ......
个人收集、整理了一些LINQ TO XML的基本方法,希望各位大虾多多指导:
/// <summary>
///Xml节点属性
/// </summary>
public class XmlAttribute
{
public XmlAttribute()
{
}
public XmlAttribute(string _key,object _value)
&nbs ......
xml文件:
<?xml version="1.0" encoding="UTF-8"?>
<mobile-list>
<mobile type="Nokia2652">
<wap2>false</wap2>
<width>115</width>
</mobile>
<mobile type="Nokia2650">
......