java xml读子节点值_java解析xml文件例子——读子节点属性值写到相应文件中 | 学步园...

xml文件格式:

1347357238_5270.PNG

java代码:

import java.io.BufferedReader;

import java.io.BufferedWriter;

import java.io.File;

import java.io.FileInputStream;

import java.io.FileWriter;

import java.io.IOException;

import java.io.InputStreamReader;

import java.text.SimpleDateFormat;

import java.util.Date;

import javax.xml.parsers.DocumentBuilder;

import javax.xml.parsers.DocumentBuilderFactory;

import javax.xml.parsers.ParserConfigurationException;

import org.w3c.dom.Document;

import org.w3c.dom.NodeList;

import org.w3c.dom.Element;

import org.w3c.dom.Node;

import org.xml.sax.InputSource;

import org.xml.sax.SAXException;

public class JXml {

private String path;

private File resultFolder;

private String childName;

private String attributeName;

DocumentBuilderFactory fac = DocumentBuilderFactory.newInstance();

public JXml(String path, String childName, String attributeName) {

super();

this.path = path;

this.childName = childName;

this.attributeName = attributeName;

}

public void go(){

File directory = new File(path);

if (!isDirectory(directory)) {

System.out.println("path路径错误或者不是文件夹");

return;

}

File[] files = getChildFiles(directory);

if (!hasFiles(files)) {

System.out.println("文件夹中没有文件");

return;

}

createDirectory(path);

for (int i = 0; i < files.length; i++) {

// 判断是否为xml文件

// 判断xml文件是否为空

readXml(files[i]);

}

}

public void readXml(File file) {

DocumentBuilder builder = null;

Document doc = null;

FileWriter fw = null;

BufferedWriter bw = null;

File resultFile = new File(resultFolder.getPath().replace("\\", "/")

+ "/" + file.getName() + "结果" + ".txt");

try {

resultFile.createNewFile();

fw = new FileWriter(resultFile);

bw = new BufferedWriter(fw);

builder = fac.newDocumentBuilder();

doc = builder.parse(new InputSource(new BufferedReader(new InputStreamReader(new FileInputStream(file),"GB18030"))));

Element root = doc.getDocumentElement();

NodeList nodes = root.getChildNodes();

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

Node node = nodes.item(i);

if (node.getNodeName().equals(childName)) {

bw.write(node.getAttributes().getNamedItem(attributeName)

.getNodeValue());

bw.newLine();

}

}

bw.flush();

bw.close();

} catch (ParserConfigurationException e) {

e.printStackTrace();

} catch (SAXException e) {

e.printStackTrace();

} catch (IOException e) {

e.printStackTrace();

}

}

public boolean isDirectory(File directory) {

if (directory.isDirectory()) {

return true;

}

return false;

}

public File[] getChildFiles(File directory) {

return directory.listFiles();

}

public void createDirectory(String path) {

Date data = new Date();

SimpleDateFormat tempDate = new SimpleDateFormat("yyyy年MM月dd日HH时mm分ss秒");

String datetime = tempDate.format(data);

String resultPath = path + "结果" + datetime;

File folder = new File(resultPath);

if (!folder.exists()) {

folder.mkdir();

}

System.out.println("结果保存在" + resultPath + "文件夹下");

this.resultFolder = folder;

}

public boolean hasFiles(File[] files){

if (!(files.length == 0)){

return true;

}

return false;

}

public static void main(String[] args) {

String path = "D:/rulexml";

String childName = "rule";

String attributeName = "name";

JXml jxml = new JXml(path, childName, attributeName);

jxml.go();

}

}

说明:代码的目的是将xml文件中的rule节点的name的值输出

将xml文件放在文件夹下,我将xml文件放在D:/rulexml文件夹下,path=文件夹路径,注意正反斜杠。文中两行注释是未完善的地方。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值