Java 如何将 String 对象(包含 XML)“转换”为现有 JSP 页面上的元素

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/851987/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 20:11:06  来源:igfitidea点击:

How to 'transform' a String object (containing XML) to an element on an existing JSP page

javaxmlxslt

提问by Lycana

Currently, I have a String object that contains XML elements:

目前,我有一个包含 XML 元素的 String 对象:

String carsInGarage = garage.getCars();

I now want to pass this String as an input/stream source (or some kind of source), but am unsure which one to choose and how to implement it.

我现在想将此字符串作为输入/流源(或某种源)传递,但不确定选择哪个以及如何实现它。

Most of the solutions I have looked at import the package: javax.xml.transform and accept a XML file (stylerXML.xml) and output to a HTML file (outputFile.html) (See code below).

我看过的大多数解决方案都导入包:javax.xml.transform 并接受一个 XML 文件(stylerXML.xml)并输出到一个 HTML 文件(outputFile.html)(见下面的代码)。

try 
{
    TransformerFactory tFactory = TransformerFactory.newInstance();
    Transformer transformer = tFactory.newTransformer(new StreamSource("styler.xsl"));

    transformer.transform(new StreamSource("stylerXML.xml"), new StreamResult(new FileOutputStream("outputFile.html")));
}
catch (Exception e)
{
    e.printStackTrace();
}

I want to accept a String object and output (using XSL) to a element within an existing JSP page. I just don't know how to implement this, even having looked at the code above.

我想接受一个 String 对象并输出(使用 XSL)到现有 JSP 页面中的一个元素。我只是不知道如何实现这一点,即使查看了上面的代码。

Can someone please advise/assist. I have searched high and low for a solution, but I just can't pull anything out.

有人可以请建议/协助。我一直在寻找解决方案,但我无法拿出任何东西。

采纳答案by bruno conde

Use a StringReaderand a StringWriter:

使用 aStringReader和 a StringWriter

try {
    StringReader reader = new StringReader("<xml>blabla</xml>");
    StringWriter writer = new StringWriter();
    TransformerFactory tFactory = TransformerFactory.newInstance();
    Transformer transformer = tFactory.newTransformer(
            new javax.xml.transform.stream.StreamSource("styler.xsl"));

    transformer.transform(
            new javax.xml.transform.stream.StreamSource(reader), 
            new javax.xml.transform.stream.StreamResult(writer));

    String result = writer.toString();
} catch (Exception e) {
    e.printStackTrace();
}

回答by jelovirt

If at some point you want the source to contain more than just a single string, or you don't want to generate the XML wrapper element manually, create a DOM document that contains your source and pass it to the transformer using a DOMSource.

如果在某些时候您希望源代码不仅仅包含单个字符串,或者您不想手动生成 XML 包装器元素,请创建一个包含源代码的 DOM 文档并使用DOMSource将其传递给转换器。

回答by Teejay

This worked for me.

这对我有用。

String str = "<my>xml</my>"    
StreamSource src = new StreamSource(new StringReader(str));
ByteArrayOutputStream baos = new ByteArrayOutputStream();
Result res = new StreamResult(baos);
transformer.transform(src, res);