资讯详情

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("没有通过");
// }
// }

}

标签: 吸收薄膜电容器xsd系列

锐单商城拥有海量元器件数据手册IC替代型号,打造 电子元器件IC百科大全!

 锐单商城 - 一站式电子元器件采购平台  

 深圳锐单电子有限公司