使用DOMSource,DOMResult& amp; XSLT

时间:2015-08-09 06:48:55

标签: java xml xslt dom

我正在尝试转换以下XML

WebClient client = new WebClient();
string resultStr = client.DownloadString(http://localhost:40786/api/Getvalues/getstring");

 <PHONEBOOK>
    <PERSON>
        <NAME>Ren1</NAME>
        <EMAIL>ren1@gmail.com</EMAIL>
        <TELEPHONE>999-999-9999</TELEPHONE>
        <WEB>www.ren1.com</WEB>
    </PERSON>
    <PERSON>
        <NAME>Ren2</NAME>
        <EMAIL>ren2@gmail.com</EMAIL>
        <TELEPHONE>999-999-9999</TELEPHONE>
        <WEB>www.ren2.com</WEB>
    </PERSON>
    <PERSON>
        <NAME>Ren3</NAME>
        <EMAIL>ren3@gmail.com</EMAIL>
        <TELEPHONE>999-999-9999</TELEPHONE>
        <WEB>www.ren3.com</WEB>
    </PERSON>
</PHONEBOOK>

使用DOMSource,DOMResult和XSLT。

使用的XSLT如下

<Names><Name>Ren1</Name><Name>Ren2</Name><Name>Ren3</Name></Names>

用于转换的Java代码:

<?xml version="1.0"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    version="1.0">
    <xsl:output omit-xml-declaration="yes" method="xml"></xsl:output>

    <xsl:template match="/">

        <Names>
            <xsl:for-each select="PHONEBOOK/PERSON">
                <Name>

                    <xsl:value-of select="NAME" />
                </Name>
            </xsl:for-each>
        </Names>

以上输出如下:

package test1;

import java.io.IOException;
import java.io.StringWriter;
import java.io.ObjectInputStream.GetField;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMResult;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import javax.xml.transform.stream.StreamSource;

import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.xml.sax.SAXException;

public class Test2 {
    public static void main(String[] args) throws TransformerException,
            ParserConfigurationException, SAXException, IOException {
        // TODO Auto-generated method stub

        //Stylesheet
        StreamSource stylesource = new StreamSource(
                "src/test1/transform_stylesheet1.xsl");

        DocumentBuilderFactory docbFactory = DocumentBuilderFactory
                .newInstance();
        DocumentBuilder dBuilder = docbFactory.newDocumentBuilder();

        //source XML
        Document sourceDoc = dBuilder.parse("src/test1/Sample1.xml");

        DOMSource source = new DOMSource(sourceDoc);

        TransformerFactory transformerFactory = TransformerFactory
                .newInstance();
        Transformer transformer = transformerFactory
                .newTransformer(stylesource);

        Document document = dBuilder.newDocument();
        DOMResult result = new DOMResult(document);

        transformer.transform(source, result);

        Node resultDoc = ((Document) result.getNode()).getDocumentElement();

        System.out.println(resultDoc.getChildNodes().getLength());

        // print the result
        StringWriter writer = new StringWriter();
        transformer.transform(new DOMSource(resultDoc), new StreamResult(writer));
        String str = writer.toString();
        System.out.println(str);


    }
}

但我希望,

3 <Names/>

我调试了代码,发现&#39; resultDoc&#39;有我期望的内容。我在打印结果时遗漏了什么吗?

1 个答案:

答案 0 :(得分:3)

您的问题是您使用相同的transformer进行样式表处理和输出。这意味着,样式表再次应用,但这次是<Names><Name>Ren1</Name>...</Names> xml。你可以想象这并没有给出你想要的结果。

将您的代码更改为:

// print the result
StringWriter writer = new StringWriter();
Transformer transformer2 = transformerFactory.newTransformer();
transformer2.transform(new DOMSource(resultDoc), new StreamResult(writer));
String str = writer.toString();
System.out.println(str);

它应该有用。

正如@Abel所提到的,你也可以一次性进行样式表处理和to String

StringWriter writer = new StringWriter();
transformer.transform(source, new StreamResult(writer));
String str = writer.toString();
System.out.println(str);

您不需要DOMResultDOMSource变量。