xslt 转换 xml
import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.StringReader; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; import javax.xml.parsers.ParserConfigurationException; import javax.xml.transform.Transformer; import javax.xml.transform.TransformerConfigurationException; import javax.xml.transform.TransformerException; import javax.xml.transform.TransformerFactory; import javax.xml.transform.TransformerFactoryConfigurationError; import javax.xml.transform.dom.DOMSource; import javax.xml.transform.stream.StreamResult; import org.w3c.dom.Document; import org.xml.sax.InputSource; import org.xml.sax.SAXException; public class DoXstl { /** * 通过传入的XML和xslt样式获得转出XML * @param tradeDoc * @param xslFile * @return */ public String generateXMLDOM(Document tradeDoc,String xslFile){ String outXml = ""; try { DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); DocumentBuilder db = dbf.newDocumentBuilder(); Document document = db.parse(xslFile); DOMSource source = new DOMSource(document); Transformer tf = TransformerFactory.newInstance().newTransformer(source); tf.setOutputProperty("encoding","GB2312");//解决中文问题 ByteArrayOutputStream bos = new ByteArrayOutputStream(); tf.transform(new DOMSource(tradeDoc), new StreamResult(bos)); outXml = bos.toString(); System.out.println("通过传入的XML和xslt样式获得转出XML"+outXml); } catch (TransformerConfigurationException e) { e.printStackTrace(); } catch (ParserConfigurationException e) { e.printStackTrace(); } catch (SAXException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } catch (TransformerFactoryConfigurationError e) { e.printStackTrace(); } catch (TransformerException e) { e.printStackTrace(); } catch (Exception e){ e.printStackTrace(); } return outXml; } /** * 通过传入的XML和xslt样式获得转出XML * @param tradeDoc * @param xslFile * @return */ public String generateXMLDOM(String xml,String xslFile){ String outXml = ""; Document tradeDoc = null; try { tradeDoc = strToDocument(xml); } catch (Exception e1) { e1.printStackTrace(); } try { DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); DocumentBuilder db = dbf.newDocumentBuilder(); Document document = db.parse(xslFile); DOMSource source = new DOMSource(document); Transformer tf = TransformerFactory.newInstance().newTransformer(source); tf.setOutputProperty("encoding","GB2312");//解决中文问题 ByteArrayOutputStream bos = new ByteArrayOutputStream(); tf.transform(new DOMSource(tradeDoc), new StreamResult(bos)); outXml = bos.toString(); System.out.println("通过传入的XML和xslt样式获得转出XML"+outXml); } catch (TransformerConfigurationException e) { e.printStackTrace(); } catch (ParserConfigurationException e) { e.printStackTrace(); } catch (SAXException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } catch (TransformerFactoryConfigurationError e) { e.printStackTrace(); } catch (TransformerException e) { e.printStackTrace(); } catch (Exception e){ e.printStackTrace(); } return outXml; } private org.w3c.dom.Document strToDocument(String xml) throws Exception { StringReader sr = new StringReader(xml); InputSource is = new InputSource(sr); DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); DocumentBuilder builder = factory.newDocumentBuilder(); org.w3c.dom.Document doc = builder.parse(is); return doc; } /* public static void main(String[] args) throws Exception{ String xmlStr = "<?xml version='1.0' encoding='GB2312'?><data></data>"; StringReader sr = new StringReader(xmlStr); InputSource is = new InputSource(sr); DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); DocumentBuilder builder=factory.newDocumentBuilder(); Document doc = builder.parse(is); String s = new DoXstl().generateXMLDOM(doc,"D:\\WorkSpace\\JAVA\\SEND.xslt"); }*/ }