使用java进行xsl转换时获取RuntimeException

时间:2015-05-22 17:13:54

标签: java xml xslt xalan

我有下面的java类,我现在正在使用xstream rite,因为你可以看到在下面的java类中我将从后面的对象生成xml我正在通过在生成的xml上应用xslt进行xsl转换我收到错误我已经完成的步骤如下所示

 InvoiceReferenceNotificationMessage invoiceReferenceNotificationMessage = new InvoiceReferenceNotificationMessage();
            invoiceReferenceNotificationMessage.setInvoiceReference("SM/829709/0315");
            invoiceReferenceNotificationMessage.setABSReference("IRMAR157311");
            invoiceReferenceNotificationMessage.setCurrency("GBP");
            invoiceReferenceNotificationMessage.setInvoiceAmount(2546);
            invoiceReferenceNotificationMessage.setPaidAmount(1245);
            invoiceReferenceNotificationMessage.setBalanceAmount(0);
            invoiceReferenceNotificationMessage.setValueDate(new Date());
            invoiceReferenceNotificationMessage.setRemarks("abc");


            InvoiceReferenceNotificationMessage invoiceReferenceNotificationMessage1 = new InvoiceReferenceNotificationMessage();
            invoiceReferenceNotificationMessage1.setInvoiceReference("SM/15");
            invoiceReferenceNotificationMessage1.setABSReference("I157311");
            invoiceReferenceNotificationMessage1.setCurrency("EUR");
            invoiceReferenceNotificationMessage1.setInvoiceAmount(255546);
            invoiceReferenceNotificationMessage1.setPaidAmount(125545);
            invoiceReferenceNotificationMessage1.setBalanceAmount(0);
            invoiceReferenceNotificationMessage1.setValueDate(new Date());
            invoiceReferenceNotificationMessage1.setRemarks("abERRc");


            Mail m = new Mail();
            String xslFilePath1 = "C:\\xsl\\rok.xsl";

            m.setInvoiceReferenceNotificationMessage(invoiceReferenceNotificationMessage);
            XStream xstream = new XStream();
            xstream.alias("arokermail",Mail.class);
            String abc = xstream.toXML(m);


            m.setInvoiceReferenceNotificationMessage(invoiceReferenceNotificationMessage1);
            xstream.alias("arokermail",Mail.class);
            String def = xstream.toXML(m);

            String t =abc+def;

所以上面的代码生成以下xml

        <arokermail>
  <invoiceReferenceNotificationMessage>
    <InvoiceReference>SM/829709/0315</InvoiceReference>
    <ABSReference>IRMAR157311</ABSReference>
    <Currency>GBP</Currency>
    <InvoiceAmount>2546.0</InvoiceAmount>
    <PaidAmount>1245.0</PaidAmount>
    <BalanceAmount>0.0</BalanceAmount>
    <ValueDate>2015-05-22 15:44:14.741 IST</ValueDate>
    <Remarks>abc</Remarks>
  </invoiceReferenceNotificationMessage>
</arokermail>
<arokermail>
  <invoiceReferenceNotificationMessage>
    <InvoiceReference>SM/15</InvoiceReference>
    <ABSReference>I157311</ABSReference>
    <Currency>EUR</Currency>
    <InvoiceAmount>255546.0</InvoiceAmount>
    <PaidAmount>125545.0</PaidAmount>
    <BalanceAmount>0.0</BalanceAmount>
    <ValueDate>2015-05-22 15:44:14.741 IST</ValueDate>
    <Remarks>abERRc</Remarks>
  </invoiceReferenceNotificationMessage>
</arokermail>

现在我还创建了一个xsl,我已经存储在我的计算机中,名为rok.xsl,我希望将其应用于生成的xml作为xsl转换的一部分,因此下面的xsl我已经存储在一个变量中,如下所示。

String xslFilePath1 = "C:\\xsl\\rok.xsl";

现在请告知如何在生成的xml上应用此xsl生成的xml存储在名为t的字符串变量中;

String t =abc+def;

下面是我通过调用函数和传递参数

尝试的
String t1 = transformXmlMessageUsingXslFile(xslFilePath1,t);

,被调用的函数如下所示

public void transform(String dataXML, String inputXSL, String outputHTML)
                throws TransformerConfigurationException, TransformerException

        {
         TransformerFactory factory = TransformerFactory.newInstance();
            StreamSource xslStream = new StreamSource(inputXSL);
            Transformer transformer = factory.newTransformer(xslStream);
           StreamSource in = new StreamSource(dataXML);
            StreamResult out = new StreamResult(outputHTML);
            transformer.transform(in, out);
            System.out.println("The generated HTML file is:" + outputHTML);

        }

    String t1 = transformXmlMessageUsingXslFile(xslFilePath1,t);

    and the methods that are being called for xsl transformation is shown below

    public static String transformXmlMessageUsingXslFile(String xslFilePath,
        String messageToTransform) {
    ByteArrayInputStream byteStream = new ByteArrayInputStream(
            messageToTransform.getBytes());
    BufferedInputStream buffMsg = new BufferedInputStream(byteStream);
    Source msgStreamSource = new StreamSource(buffMsg);
    String transformedXML = null;
    try {
        Transformer xsltTransformer = buildTransformer(xslFilePath);
        transformedXML = transformMessageUsingXslt(msgStreamSource,
                xsltTransformer);

    } catch (Exception e) {
        e.printStackTrace();
    }
    return transformedXML;
}


private static String transformMessageUsingXslt(Source msgStreamSource,
        Transformer xsltTransformer) throws Exception {

    // StringWriter to hold output
    StringWriter strWriter = new StringWriter();

    // Result stream
    Result xmlOutput = new StreamResult(strWriter);

    // String to hold transformed xml
    String transformedXML = null;

    try {
        // perform XSL transformation
        xsltTransformer.transform(msgStreamSource, xmlOutput);

        // get the transformed xml
        transformedXML = strWriter.toString();
        System.out.println("*######********#####***"); 
        System.out.println(transformedXML);

    } finally {
        strWriter.close();
    }
    System.out.println("************"); 

    return transformedXML;
}

public static Transformer buildTransformer(String xslFilePath) {
    Source xsltSource = new StreamSource(xslFilePath);
    TransformerFactory transFact = TransformerFactory.newInstance();
    Transformer xslTransformer = null;
    try {
        xslTransformer = transFact.newTransformer(xsltSource);
        System.out.println("##########");
        System.out.println(xslTransformer.toString());
    } catch (TransformerConfigurationException e) {
        e.printStackTrace();
    }
    return xslTransformer;
}

我专门针对该行执行的错误是..

// perform XSL transformation
            xsltTransformer.transform(msgStreamSource, xmlOutput);




 com.sun.org.apache.xalan.internal.xsltc.trax.TransformerImpl@161dfb5
ERROR:  'The markup in the document following the root element must be well-formed.'
ERROR:  'com.sun.org.apache.xml.internal.utils.WrappedRuntimeException: The markup in the document following the root element must be well-formed.'
javax.xml.transform.TransformerException: javax.xml.transform.TransformerException: com.sun.org.apache.xml.internal.utils.WrappedRuntimeException: The markup in the document following the root element must be well-formed.
    at com.sun.org.apache.xalan.internal.xsltc.trax.TransformerImpl.transform(Unknown Source)
    at com.sun.org.apache.xalan.internal.xsltc.trax.TransformerImpl.transform(Unknown Source)

我更新了xsl所以我的rok.xsl如下所示..

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

    <xsl:template match="/">
        <html>
            <body>
                Hello,
                <br>
                </br>
                <br>
                </br>
                Please be advised of the following payment details
                    <font color="black" face="Arial" size="2" >
                    <br>
                        <br>

                            <table border="1" cellpadding="3" cellspacing="0">
                                <tr bgcolor="lightblue">
                                     <td nowrap="nowrap">Invoice Reference</td>
                                     <td nowrap="nowrap">ABSReference</td>
                                     <td nowrap="nowrap">Currency</td>
                                     <td nowrap="nowrap">Invoice Amount</td>
                                     <td nowrap="nowrap">Paid Amount</td>
                                     <td nowrap="nowrap">Balance Amount</td>
                                     <td nowrap="nowrap">Value Date</td>
                                     <td nowrap="nowrap">Remarks</td>

                                </tr>
                                <xsl:for-each select="arokermail/invoiceReferenceNotificationMessage">
                                    <tr>
                                        <td nowrap="nowrap"><xsl:value-of select="InvoiceReference" /></td>
                                        <td nowrap="nowrap"><xsl:value-of select="ABSReference" /></td>
                                        <td nowrap="nowrap"><xsl:value-of select="Currency" /></td>
                                        <td nowrap="nowrap"><xsl:value-of select="InvoiceAmount" /></td>
                                        <td nowrap="nowrap"><xsl:value-of select="PaidAmount" /></td>
                                        <td nowrap="nowrap"><xsl:value-of select="BalanceAmount" /></td>
                                        <td nowrap="nowrap"><xsl:value-of select="ValueDate" /></td>
                                        <td nowrap="nowrap"><xsl:value-of select="Remarks" /></td>
                                        </tr>
                                </xsl:for-each>
                                <TR>

                                </TR>
                            </table>
                        </br>
                    </br>
                    <p>

-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
</br>
</br>                                   
</p>
                </font>
            </body>
        </html>
    </xsl:template>
</xsl:stylesheet>

更新邮件类如下所示..

public class Mail {


    private  List<InvoiceReferenceNotificationMessage> invoiceReferenceNotificationMessage = new ArrayList<InvoiceReferenceNotificationMessage>();

    public List<InvoiceReferenceNotificationMessage> getInvoiceReferenceNotificationMessages() {
        return invoiceReferenceNotificationMessage;
    }

    public void addInvoiceReferenceNotificationMessage(InvoiceReferenceNotificationMessage invoiceReferenceNotificationMessage) {
        this.invoiceReferenceNotificationMessage.add(invoiceReferenceNotificationMessage);
    }


    private  List<InvoiceReferenceNotificationMessage> InvoiceReferenceNotificationMessage = 
new ArrayList<InvoiceReferenceNotificationMessage>();
}

1 个答案:

答案 0 :(得分:0)

你的XML有两个根源!一个XML文件只能有一个顶级元素,并且您有两个arokermail元素。

请参阅http://en.wikipedia.org/wiki/Root_element

  

每个XML文档只有一个根元素。

相关问题