xsd校验xml工具

xsd校验xml工具,第1张

xsd校验xml工具 xml校验工具

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

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

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

工具使用 选择对应的xsd及xml文件

点击确定后获取校验结果

失败则显示失败原因 代码 主面板
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;


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;


public class XmlValidate {

	
	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;
	}

	
	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;
	}

	
	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;
	}

	
	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:\Users435\Desktop\ccms.903.001.02.xml");
//		FileInputStream xsd = new FileInputStream("C:\Users435\Desktop\ccms.903.001.02.xsd");
//		try {
//			isPassed = validate(xml, xsd);
//		} catch (SAXException e) {
//
//		}
//		if (isPassed) {
//			System.out.println("通过");
//		} else {
//			System.out.println("没有通过");
//		}
//	}

}

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/zaji/5637197.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-16
下一篇 2022-12-16

发表评论

登录后才能评论

评论列表(0条)

保存