遍历所有xml节点


package com.huawei.entity;

/**
* xml节点数据结构
*/
public class Leaf
{
// 节点属性
private String xattribute;

// 节点PATH
private String xpath;

// 节点值
private String value;

public Leaf(String xattribute, String xpath, String value)
{
this.xattribute = xattribute;
this.xpath = xpath;
this.value = value;
}

public String getXpath()
{
return xpath;
}

public void setXpath(String xpath)
{
this.xpath = xpath;
}

public String getValue()
{
return value;
}

public void setValue(String value)
{
this.value = value;
}

public String getXattribute()
{
return xattribute;
}

public void setXattribute(String xattribute)
{
this.xattribute = xattribute;
}
}

===================

package com.huawei.utils;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
import org.dom4j.tree.DefaultAttribute;

import com.huawei.entity.Leaf;

public class ParseXML
{
//存储xml元素信息的容器
private static ArrayList<Leaf> elemList = new ArrayList<Leaf>();

public static void main(String args[])
throws DocumentException
{
ParseXML test = new ParseXML();
String path = "D:/4Tools/crm.xml";
// 读取XML文件
SAXReader reader = new SAXReader();
Document doc = reader.read(path);
// 获取XML根元素
Element root = doc.getRootElement();
test.getElementList(root);
String result = test.getListString();
//去掉消息头
result = result.replace("/soapenv:Envelope/soapenv:Header", "");
//去掉前缀消息体
result = result.replace("/soapenv:Envelope/soapenv:Body/", "");
result = result.replace("/","." );
result = result.replace("", "");
WriteReslut wr = new WriteReslut();
wr.outputResult("D:/4Tools/", "result.txt", result);
System.out.println("-----------解析结果------------\n" + result);
}
/**
* 获取节点所有属性值
*/
@SuppressWarnings("unchecked")
public String getNoteAttribute(Element element)
{
String xattribute = "";
DefaultAttribute e = null;
List list = element.attributes();
for (int i = 0; i < list.size(); i++)
{
e = (DefaultAttribute)list.get(i);
//System.out.println("name = " + e.getName() + ", value = " + e.getText());
xattribute += " [name = " + e.getName() + ", value = " + e.getText() + "]";
}
return xattribute;
}

/**
* 递归遍历方法
* <功能详细描述>
* @param element
* @see [类、类#方法、类#成员]
*/
@SuppressWarnings("unchecked")
public void getElementList(Element element)
{
List elements = element.elements();
// 没有子元素
if (elements.isEmpty())
{
String xpath = element.getPath();
String value = element.getTextTrim();
elemList.add(new Leaf(getNoteAttribute(element), xpath, value));
}
else
{
// 有子元素
Iterator it = elements.iterator();
while (it.hasNext())
{
Element elem = (Element)it.next();
// 递归遍历
getElementList(elem);
}
}
}

@SuppressWarnings("unchecked")
public String getListString()
{
StringBuffer sb = new StringBuffer();
for (Iterator it = elemList.iterator(); it.hasNext();)
{
Leaf leaf = (Leaf)it.next();
sb.append(leaf.getXpath());
if (!"".equals(leaf.getXattribute()))
{
sb.append(", Attribute: ").append(leaf.getXattribute());
}
sb.append("\n");
}
return sb.toString();
}
}

=========

package com.huawei.utils;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;

public class WriteReslut
{
//输出流
BufferedWriter bw;

public void outputResult(String path,String name,String content)
{
File file = new File(path + name);
try {
bw = new BufferedWriter(new FileWriter(file));
bw.write(content);
bw.flush();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally
{
try {
bw.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}

======

package com.huawei.utils;

import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;

import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

public class GUI {
private String filePath;
private File srcFile;
private JFrame jFrame = null;
private JButton choseButton = null;
private JButton computeButton = null;
private JTextField jtf = null;
private JLabel la = null;
private JFileChooser jfc = null;

private ButtonListener buttLis = new ButtonListener();

public void Frame() {
jFrame = new JFrame("分析报文节点");
jFrame.setLayout(new FlowLayout());
jtf = new JTextField(15);
choseButton = new JButton("选择");
computeButton = new JButton("分析节点");
la = new JLabel("请选择文件路径:");
jfc = new JFileChooser();
jfc.setCurrentDirectory(new File("D:\\"));
jfc.setDialogTitle("选择文件");
jfc.setDragEnabled(false);

//添加控件
jFrame.add(la);
jFrame.add(jtf);
jFrame.add(choseButton);
jFrame.add(computeButton);
choseButton.addActionListener(buttLis);
computeButton.addActionListener(buttLis);

//设置
jFrame.setBounds(250, 200, 600, 350);//frame初始显示位置
jFrame.setSize(400, 100);
jFrame.setVisible(true);
jFrame.setResizable(false);//将frame设置成不能改变大小
jFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}

/**
* main 函数
*/
public static void main(String[] args) {
GUI gui = new GUI();
gui.Frame();
}

/**
* 监听类
*/
class ButtonListener implements ActionListener {

@Override
public void actionPerformed(ActionEvent event) {
// TODO Auto-generated method stub
if (event.getSource().equals(choseButton)) {
// 设置其只能选择文件
jfc.setFileSelectionMode(JFileChooser.FILES_ONLY);
int state = jfc.showOpenDialog(null);
if (state == 1) {
return;
} else {
srcFile = jfc.getSelectedFile();
jtf.setText(srcFile.getAbsolutePath());
filePath = srcFile.getPath();
}
} else {
if (filePath == null) {
JOptionPane.showMessageDialog(jFrame, "请先选择文件");
} else {
ParseXML test = new ParseXML();
// 读取XML文件
SAXReader reader = new SAXReader();
Document doc;
try {
doc = reader.read(filePath);
// 获取XML根元素
Element root = doc.getRootElement();
test.getElementList(root);
String result = test.getListString();
//去掉消息头
result = result.replace(
"/soapenv:Envelope/soapenv:Header", "");
//去掉前缀消息体
result = result.replace(
"/soapenv:Envelope/soapenv:Body/", "");
result = result.replace("/", ".");
result = result.replace("", "");
WriteReslut wr = new WriteReslut();
wr.outputResult(
filePath.replace(srcFile.getName(), ""),
"分析结果.txt", result);
JOptionPane.showMessageDialog(jFrame, "分析完毕!");
} catch (DocumentException e)
{
// TODO Auto-generated catch block
JOptionPane.showMessageDialog(jFrame, e);
}
}

}
}

}
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值