Use XSLT to convert XML2XML

Source: Internet
Author: User
Tags xsl xslt

In EDI (Electronic Data Exchange) systems, this is often the case that the XML document defined in the system is in a format, the external system is defined in another format, or the formats of different external systems are not the same. For example, if a person is defined as <employee> in some places, other organizations use <worker> or <associate>. ① At this time, we need to convert XML into XML format.

XSLT stands out. XSLT is (EXtensibleSTylesheetLAnguage extended style sheet language) is a language that converts XML into other formats. For more information, see XSLT documents on the Internet.

If you use Java, you are lucky. Java provides a set of APIs that support XSLT usage. You can easily perform conversion without using third-party libraries. For conversion, you only need to prepare the source file (converted XML), the conversion template (XSLT file), and a piece of Java code.

Example:

Source file:

<?xml version="1.0" encoding="UTF-8"?><discussionForumHome><messageBoard id="1" name="Java Programming"/><messageBoard id="2" name="XML Programming"/><messageBoard id="3" name="XSLT Questions"/></discussionForumHome>

Template:

<?xml version="1.0" encoding="UTF-8"?><xsl:stylesheetversion="1.0"xmlns:xsl="http://www.w3.org/1999/XSL/Transform"><xsl:output method="html"/><!-- match the document root --><xsl:template match="/">

Java code

import java.io.File;import java.io.IOException;import java.net.URISyntaxException;import javax.xml.transform.Source;import javax.xml.transform.Transformer;import javax.xml.transform.TransformerException;import javax.xml.transform.TransformerFactory;import javax.xml.transform.stream.StreamResult;import javax.xml.transform.stream.StreamSource;public class TestMain {    public static void main(String[] args) throws IOException, URISyntaxException, TransformerException {        TransformerFactory factory = TransformerFactory.newInstance();        Source xslt = new StreamSource(new File("D:\\dev\\learn\\discussionForumHome.xslt"));        Transformer transformer = factory.newTransformer(xslt);        Source text = new StreamSource(new File("D:\\dev\\learn\\discussionForumHome.xml"));        transformer.transform(text, new StreamResult(new File("D:\\dev\\learn\\output.xml")));    }}

TIP 1. The output file name is incorrect.

If you directly use StreamResult for file output, the file name will change to % ASCII format, for example

Transformer. transform (text, new StreamResult (new File ("D: \ dev \ learn \ puppy. xml ")));

The output file name is:

% E5 % B0 % 8F % E7 % 8B % 97.xml

Tracking the code of StreamResult at this time, we found that StreamResult uses the file ASCII In the constructor as the SystemID.

SetSystemId (f. toURI (). toASCIIString ());

In this case, you can set OutputStream.

StreamResult sr = new StreamResult (); sr. setOutputStream (new FileOutputStream (new File ("D: \ dev \ learn \ puppy. xml "); transformer. transform (text, sr );

 

Note ①. The example is taken from Java and XSLT, and the content of XML files is the same.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.