创建解析XML文件的三种解析方法(一)

前端之家收集整理的这篇文章主要介绍了创建解析XML文件的三种解析方法(一)前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

用w3c解析

package w3c;

import java.io.File;

import java.io.FileOutputStream;

import java.util.Random;

import javax.xml.parsers.DocumentBuilder;

import javax.xml.parsers.DocumentBuilderFactory;

import javax.xml.transform.OutputKeys;

import javax.xml.transform.Transformer;

import javax.xml.transform.TransformerFactory;

import javax.xml.transform.dom.DOMSource;

import javax.xml.transform.stream.StreamResult;

import org.w3c.dom.Document;

import org.w3c.dom.Element;

import org.w3c.dom.NodeList;

publicclass w3c {

publicstaticvoid main(String[] args) throws Exception{

createXml();

}

//创建XML文档并创建节点

publicstaticvoid createXml() throws Exception{

//获取XML解析器工厂类

DocumentBuilderFactory dbf =DocumentBuilderFactory.newInstance();

//获取XML解析器

DocumentBuilder db = dbf.newDocumentBuilder();

//获取操作的document对象

Document document = db.newDocument();

//设置XML文件的版本

document.setXmlVersion("1.0");

//创建根节点

Element root = document.createElement("students");

//将根节点添加document对象中

document.appendChild(root);

//声明一个姓名的数组

String nameList[] = {"吕布","赵云","马超","张飞","关羽","许褚","孙策","周瑜","夏侯渊","张颌","于禁","黄忠","典韦","曹仁","程普"};

String sexList[] = {"",""};

//循环添加数据

for(int i = 0 ;i < 20 ; i++){

//创建根元素节点

Element rootelement = document.createElement("student");

//声明参数ID

String id ="";

//循环生成一个9为的随机ID

for(int j = 0; j< 9 ; j ++ ){

id += new Random().nextInt(8)+1;

}

//设置根元素节点的属性

rootelement.setAttribute("id",id);

//将根元素节点添加到根节点中

root.appendChild(rootelement);

//设置元素节点

Element name = document.createElement("name");

Element sex = document.createElement("sex");

Element age = document.createElement("age");

Element phone = document.createElement("phone");

//给元素节点赋值

name.setTextContent(nameList[new Random().nextInt(nameList.length)]);

sex.setTextContent(sexList[new Random().nextInt(sexList.length)]);

age.setTextContent(new Random().nextInt(20)+20+"");

String tel ="";

for(int k = 0; k< 7 ; k++ ){

tel += new Random().nextInt(9);

}

phone.setTextContent("0756-"+tel);

//将元素节点添加到根元素节点中

rootelement.appendChild(name);

rootelement.appendChild(sex);

rootelement.appendChild(age);

rootelement.appendChild(phone);

}

//开始把Document映射到文件

TransformerFactory transFactory = TransformerFactory.newInstance();

Transformer transFormer = transFactory.newTransformer();

//生成XML文件

File file = new File("w3c.xml");

//判断XML文件存不存在,如果不存在则创建

if(file.exists()){

file.createNewFile();

}

//设置是否添加空格

transFormer.setOutputProperty(OutputKeys.INDENT,"yes");

//设置输出文件的格式

transFormer.setOutputProperty(OutputKeys.ENCODING,"utf-8");

//输出XML文件

transFormer.transform(new DOMSource(document),new StreamResult(new FileOutputStream(file)));

//输出XML文件的路径

System.out.println(file.getAbsolutePath());

}

//解析XML文档

publicstaticvoid resolving() throws Exception{

//获取XML解析器工厂类

DocumentBuilderFactory dbf =DocumentBuilderFactory.newInstance();

//获取XML解析器

DocumentBuilder db = dbf.newDocumentBuilder();

//获取操作的document对象

Document document = db.parse(new File("w3c.xml"));

//获取XML文件的根元素节点

NodeList root = document.getElementsByTagName("student");

//循环根元素节点信息

for(int i = 0 ; i < root.getLength() ;i++){

Element student = (Element)root.item(i);

System.err.println("id = "+student.getAttribute("id")+" name = "

+student.getElementsByTagName("name").item(0).getTextContent()+" sex = "

+student.getElementsByTagName("sex").item(0).getTextContent()+" age = "

+student.getElementsByTagName("age").item(0).getTextContent()+" phone = "

+student.getElementsByTagName("phone").item(0).getTextContent());

}

}

}

原文链接:https://www.f2er.com/xml/298305.html

猜你在找的XML相关文章