java 中的 xml 解析_java中的xml 解析xml(上)

一、解析 xml 文件(读、写、修改、删除)

在 java 中解析xml 有一下几种方式:

a、dom

是以文档结构的方式解析 xml(特点:一次性将所有xml的内容读取到内存中,然后依次读取)

b、sax

事件驱动:依次的解析xml文件,而不是一次性将所有内容读取到内存中

有两个基于解析xml封装的框架

a、jdom

b、dom4j

二、使用jdom解析xml文件

步骤:

a 、找到对应的jar包

b、 找到api

完成:在E盘中创建一个 student.xml文件,包括1 个学生的信息

三、 使用dom4j的方式解析xml

步骤和使用jdom解析xml文件一样

四、下面用jdom解析xml文件 (首先要获得 jdom.jar 包)

import java.io.File;

import java.io.FileNotFoundException;

import java.io.FileOutputStream;

import java.io.IOException;

import org.jdom.Document;

import org.jdom.Element;

import org.jdom.output.XMLOutputter;

/**

*创建一个名为student的xml文件

*/

public class Write {

public static void main(String[] args) throws FileNotFoundException, IOException {

//创建根节点元素

Element root=new Element("root");

//创建元素

Element student=new Element("student");

Element name=new Element("name");

Element sex=new Element("sex");

Element age=new Element("age");

//设置关系

student.addContent(name);

student.addContent(sex);

student.addContent(age);

//设置属性

student.setAttribute("stuNo", "1001");

//设置内容

name.setText("张三");

sex.setText("男");

age.setText("20");

//添加到根节点中

root.addContent(student);

//创建文档对象

Document doc=new Document(root);

//将文档对象doc写入到文件中去

XMLOutputter o=new XMLOutputter();

//此处会有异常

o.output(doc, new FileOutputStream(new File("d:/student.xml")));

System.out.println("成功");

}

}

/*

* 读取xml文件,如读取该路径下的xml文件(d:/student.xml)

*/

import java.io.File;

import java.io.IOException;

import java.util.List;

import org.jdom.Document;

import org.jdom.Element;

import org.jdom.JDOMException;

import org.jdom.input.SAXBuilder;

public class Read {

public static void main(String[] args) throws JDOMException, IOException {

//创建解析器(读取xml的对象)

SAXBuilder b=new SAXBuilder();

//得到文档对象(找到要解析的文件)

Document doc=b.build(new File("d:/student.xml"));

//获得根节点

Element root=doc.getRootElement();

//获得子元素集合

List list=root.getChildren();

//用增强for循环输出

for(Element e:list){

System.out.println(e.getAttributeValue("stuNo")+"--------"+e.getChildText("name"));

}

}

}

/*

* 向指定的xml文件中添加东西

*/

import java.io.File;

import java.io.FileOutputStream;

import java.io.IOException;

import org.jdom.Document;

import org.jdom.Element;

import org.jdom.JDOMException;

import org.jdom.input.SAXBuilder;

import org.jdom.output.XMLOutputter;

public class Add {

public static void main(String[] args) throws JDOMException, IOException {

//创建解析器(读取xml文件的对象)

SAXBuilder b=new SAXBuilder();

//获得文档对象,找到要解析的xml文件

Document doc=b.build(new File("d:/student.xml"));

//创建节点

Element student=new Element("student");

Element name=new Element("name");

Element sex=new Element("sex");

Element age=new Element("age");

//设置关系

student.addContent(name);

student.addContent(sex);

student.addContent(age);

//设置属性

student.setAttribute("stuNo", "1002");

//设置值

name.setText("李四");

sex.setText("女");

age.setText("21");

//获得根节点

Element root=doc.getRootElement();

root.addContent(student);

//将新数据重新写入到文件中

XMLOutputter o=new XMLOutputter();

o.output(doc, new FileOutputStream(new File("d:/student.xml")));

System.out.println("写入成功");

}

}

/*

* 修改和删除

*/

import java.io.File;

import java.io.FileOutputStream;

import java.io.IOException;

import java.util.List;

import org.jdom.Document;

import org.jdom.Element;

import org.jdom.JDOMException;

import org.jdom.input.SAXBuilder;

import org.jdom.output.XMLOutputter;

public class Update {

public static void main(String[] args) throws JDOMException, IOException {

//创建解析器(读取xml的对象)

SAXBuilder b=new SAXBuilder();

//得到文档对象(找到要解析的文件)

Document doc=b.build(new File("d:/student.xml"));

//获得根节点

Element root=doc.getRootElement();

//获得子元素集合

List list=root.getChildren();

for(Element e:list){

//说明 e 就是要修改的 student 应该找到 e中的name 和sex 元素

if(e.getAttributeValue("stuNo").equals("1002")){

e.getChild("name").setText("王小红");

e.getChild("age").setText("18");

//删除某个节点

//root.removeContent(e);

break;

}

}

//从新将root 设置到 doc中

doc.setRootElement(root);

//将新数据重新写入到文件中

XMLOutputter o=new XMLOutputter();

o.output(doc, new FileOutputStream(new File("d:/student.xml")));

System.out.println("修改成功");

}

}

//dom4j 下回再分解

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
package com.hexiang.utils; 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; /** * 本类是专门解析XML文件的,主要用于为系统读取自己的配置文件时提供最方便的解析操作 * @author HX * */ public class XmlManager { /** * 得到某节点下某个属性的值 * @param element 要获取属性的节点 * @param attributeName 要取值的属性名称 * @return 要获取的属性的值 * @author HX_2010-01-12 */ public static String getAttribute( Element element, String attributeName ) { return element.getAttribute( attributeName ); } /** * 获取指定节点下的文本 * @param element 要获取文本的节点 * @return 指定节点下的文本 * @author HX_2010-01-12 */ public static String getText( Element element ) { return element.getFirstChild().getNodeValue(); } /** * 解析某个xml文件,并在内存创建DOM树 * @param xmlFile 要解析XML文件 * @return 解析某个配置文件后的Document * @throws Exception xml文件不存在 */ public static Document parse( String xmlFile ) throws Exception { // 绑定XML文件,建造DOM树 DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); DocumentBuilder db = dbf.newDocumentBuilder(); Document domTree = db.parse( xmlFile ); return domTree; } /** * 获得某节点下的某个子节点(指定子节点名称,和某个属性的值) * 即获取parentElement下名字叫childName,并且属性attributeName的值为attributeValue的子结点 * @param parentElement 要获取子节点的那个父节点 * @param childName 要获取的子节点名称 * @param attributeName 要指定的属性名称 * @param attributeValue 要指定的属性的值 * @return 符合条件的子节点 * @throws Exception 子结点不存在或有多个符合条件的子节点 * @author HX_2008-12-01 */ public static Element getChildElement( Element parentElement, String childName, String attributeName, String attributeValue ) throws Exception { NodeList list = parentElement.getElementsByTagName( childName ); int count = 0; Element curElement = null; for ( int i = 0 ; i < list.getLength() ; i ++ ) { Element child = ( Element )list.item( i ); String value = child.getAttribute( attributeName ); if ( true == value.equals( attributeValue ) ) { curElement =
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值