xsd校验xml工具

xml校验工具

xml校验工具,用于校验xml报文是否符合xsd规范

在线网站也可以校验
https://www.xmlvalidation.com/

源码及工具下载地址
https://download.csdn.net/download/qq_21271511/54181672

工具使用

选择对应的xsd及xml文件

在这里插入图片描述

点击确定后获取校验结果

在这里插入图片描述

失败则显示失败原因

代码

主面板

/**   
 * @Title: MainFrame.java 
 * @Package com.agree.mainFrame 
 * @Description: 主面板
 * @author lyz 
 * @date 2015-12-5 下午12:13:42 
 * @version V1.0   
 */
package xmlValidate.com.agree.main;

import java.awt.BorderLayout;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.FileInputStream;

import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.filechooser.FileNameExtensionFilter;

import xmlValidate.com.agree.xmlUtil.XmlValidate;

/**
 * @ClassName: MainFrame
 * @Description: 主面板
 * @author lyz 
 * @date 2015-12-5 下午12:13:42
 * 
 */
public class MainFrame extends JFrame {
	private static final long serialVersionUID = 1L;
	private JFrame jf = null;
	private JPanel jPanel;
	private JTextField text_xsd = new JTextField("", 20);// xsd文件路径
	private JTextField text_xml = new JTextField("", 20);// xml文件路径

	// 得到屏幕分辨率
	int width = Toolkit.getDefaultToolkit().getScreenSize().width;
	int height = Toolkit.getDefaultToolkit().getScreenSize().height;

	public MainFrame() {
		jf = new JFrame();
		jf.setVisible(true);
		jf.setTitle("校验工具");
		jf.setSize(400, 180);

		jf.setLocation((width - 400) / 2, (height - 300) / 2);
		jf.setResizable(false);// 窗体不可调整
		jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);// 窗体关闭时关闭应用程序

		jPanel = new JPanel();

		JLabel label1 = new JLabel("请选择xsd文件");
		JButton input = new JButton("浏览");

		JLabel label2 = new JLabel("请选择xml文件");
		JButton output = new JButton("浏览");

		JButton submit = new JButton("确定");
		JButton exit = new JButton("退出");

		jPanel.add(label1);
		jPanel.add(text_xsd);
		jPanel.add(input);

		jPanel.add(label2);
		jPanel.add(text_xml);
		jPanel.add(output);

		jPanel.add(submit, BorderLayout.SOUTH);
		jPanel.add(exit, BorderLayout.SOUTH);

		jf.add(jPanel);

		input.addActionListener(new MouseAction("input"));
		output.addActionListener(new MouseAction("output"));
		submit.addActionListener(new MouseAction("submit"));
		exit.addActionListener(new MouseAction("exit"));
	}

	class MouseAction implements ActionListener {
		private String onclick = "";

		public MouseAction(String str) {
			onclick = str;
		}

		@Override
		public void actionPerformed(ActionEvent e) {
			// 文件选择
			if ("input".equals(onclick)) {
				JFileChooser chooser = new JFileChooser();
				chooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
				chooser.setFileFilter(new FileNameExtensionFilter("xsd files", "xsd"));
				int returnval = chooser.showDialog(jf, "请选择文件");
				if (returnval == JFileChooser.APPROVE_OPTION) {
					String inputFileName = chooser.getSelectedFile().getPath();
					text_xsd.setText(inputFileName);
				}
			} else if ("output".equals(onclick)) {
				JFileChooser chooser = new JFileChooser();
				chooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
				chooser.setCurrentDirectory(new File("."));
				chooser.setFileFilter(new FileNameExtensionFilter("xml files", "xml"));
				int returnval = chooser.showDialog(jf, "请选择文件");
				if (returnval == JFileChooser.APPROVE_OPTION) {
					String outputFileName2 = chooser.getSelectedFile().getPath();
					text_xml.setText(outputFileName2);
				}
			} else if ("submit".equals(onclick)) {
				System.out.println(text_xsd.getText());
				System.out.println(text_xml.getText());
				boolean isPassed = false;
				try {
					FileInputStream xsd = new FileInputStream(text_xsd.getText());
					FileInputStream xml = new FileInputStream(text_xml.getText());

					isPassed = XmlValidate.validate(xml, xsd);
				} catch (Exception err) {
					JOptionPane.showMessageDialog(null, "校验失败!" + err, "消息", JOptionPane.ERROR_MESSAGE);
				}

				if (!"".equals(text_xsd.getText()) && !"".equals(text_xml.getText())) {
				}

				if (isPassed) {
					JOptionPane.showMessageDialog(null, "校验成功!", "消息", JOptionPane.INFORMATION_MESSAGE);
				} else {
					JOptionPane.showMessageDialog(null, "校验失败!", "消息", JOptionPane.ERROR_MESSAGE);
				}
			} else if ("exit".equals(onclick)) {
				jf.dispose();
			}
		}
	}

	public static void main(String[] args) {
		MainFrame mf = new MainFrame();
	}
}

工具类

package xmlValidate.com.agree.xmlUtil;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;

import javax.xml.XMLConstants;
import javax.xml.transform.Source;
import javax.xml.transform.stream.StreamSource;
import javax.xml.validation.Schema;
import javax.xml.validation.SchemaFactory;
import javax.xml.validation.Validator;

import org.xml.sax.SAXException;

/**
 * @author lyz
 *
 */
public class XmlValidate {

	/**
	 * @param isXml
	 * @param isXsd
	 * @return
	 * @throws SAXException
	 * @throws IOException
	 */
	public static boolean validate(InputStream isXml, InputStream isXsd) throws SAXException, IOException {
		boolean flag = false;
		SchemaFactory sf = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
		try {
			Source xsd = new StreamSource(isXsd);
			Schema schema = sf.newSchema(xsd);
			Validator validator = schema.newValidator();
			validator.validate(new StreamSource(isXml));
			flag = true;

		} catch (SAXException e) {
			flag = false;
			throw new SAXException(e.getMessage());

		} catch (IOException e) {
			flag = false;
			throw new IOException(e.getMessage());
		} finally {
			if (isXml != null) {
				isXml.close();
				isXml = null;
			}
			if (isXsd != null) {
				isXsd.close();
				isXsd = null;
			}
		}
		return flag;
	}

	/**
	 * @param isXml
	 * @param isXsd
	 * @return
	 * @throws SAXException
	 * @throws IOException
	 */
	public static boolean validate(InputStream isXml, File isXsd) throws SAXException, IOException {
		boolean flag = false;
		SchemaFactory sf = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
		try {
			Source xsd = new StreamSource(isXsd);
			Schema schema = sf.newSchema(xsd);
			Validator validator = schema.newValidator();
			validator.validate(new StreamSource(isXml));
			flag = true;

		} catch (SAXException e) {
			e.printStackTrace();
			flag = false;
			throw new SAXException(e.getMessage());
		} catch (IOException e) {
			e.printStackTrace();
			flag = false;
			throw new IOException(e.getMessage());
		} finally {
			if (isXml != null) {
				isXml.close();
				isXml = null;
			}
		}
		return flag;
	}

	/**
	 * @param isXml
	 * @param xsd
	 * @return
	 * @throws SAXException
	 * @throws IOException
	 */
	public static boolean validate(InputStream isXml, URL xsd) throws SAXException, IOException {
		boolean flag = false;
		SchemaFactory sf = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
		try {
			Schema schema = sf.newSchema(xsd);
			Validator validator = schema.newValidator();
			validator.validate(new StreamSource(isXml));
			flag = true;

		} catch (SAXException e) {
			e.printStackTrace();
			flag = false;
			throw new SAXException(e.getMessage());
		} catch (IOException e) {
			e.printStackTrace();
			flag = false;
			throw new IOException(e.getMessage());
		} finally {
			if (isXml != null) {
				isXml.close();
				isXml = null;
			}
		}
		return flag;
	}

	/**
	 * @param xml
	 * @param xsd
	 * @return
	 * @throws SAXException
	 * @throws IOException
	 */
	public static boolean validate(File xml, File xsd) throws SAXException, IOException {
		boolean flag = false;
		SchemaFactory sf = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
		try {
			Schema schema = sf.newSchema(xsd);
			Validator validator = schema.newValidator();
			validator.validate(new StreamSource(xml));
			flag = true;
		} catch (SAXException e) {
			e.printStackTrace();
			flag = false;
			throw new SAXException(e.getMessage());
		} catch (IOException e) {
			e.printStackTrace();
			flag = false;
			throw new IOException(e.getMessage());
		}
		return flag;
	}


//	public static void main(String[] args)
//			throws JAXBException, DocumentException, UnsupportedEncodingException, IOException {
//		boolean isPassed = false;
//		FileInputStream xml = new FileInputStream("C:\\Users\\14435\\Desktop\\ccms.903.001.02.xml");
//		FileInputStream xsd = new FileInputStream("C:\\Users\\14435\\Desktop\\ccms.903.001.02.xsd");
//		try {
//			isPassed = validate(xml, xsd);
//		} catch (SAXException e) {
//
//		}
//		if (isPassed) {
//			System.out.println("通过");
//		} else {
//			System.out.println("没有通过");
//		}
//	}

}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

忙碌的菠萝

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值