gpt4 book ai didi

java - 使用 DOMResult 进行 XML 转换

转载 作者:数据小太阳 更新时间:2023-10-29 02:22:08 25 4
gpt4 key购买 nike

当我取消注释以下代码中的一行时,我得到了 java.lang.NullPointerException:

private static Document XSLT(String name) throws Exception{
File xsl = new File("data/" + name + ".xsl");
File xml = new File("data/" + name + ".xml");
DocumentBuilder db = DocumentBuilderFactory.newInstance().newDocumentBuilder();
Document document = db.newDocument();
DOMSource input = new DOMSource(db.parse(xml));
StreamResult output = new StreamResult(System.out); //That works but its not what I want!
//DOMResult output = new DOMResult(document); //that gives the null pointer exception
TransformerFactory.newInstance().newTransformer(new StreamSource(xsl)).transform(input, output);
return document;
}

我的意图是创建一个类似的方法来帮助我使用 XSLT 和 Java 语言转换 XML 文件。毕竟我应该在将其保存到文件之前完成所有转换。

如何从该方法中获取文档?

我的意思是,我想在不将转换后的文档保存到文件中以便能够加载它的情况下执行此操作,我只想在“内存”中执行此操作。

谢谢你:)

更新

让我发布异常,以便您更好地帮助我。

Exception in thread "main" javax.xml.transform.TransformerException: java.lang.NullPointerException
at com.sun.org.apache.xalan.internal.xsltc.trax.TransformerImpl.transform(TransformerImpl.java:736)
at com.sun.org.apache.xalan.internal.xsltc.trax.TransformerImpl.transform(TransformerImpl.java:340)
at test.Main.XSLT(Main.java:393)
at test.Main.test(Main.java:342)
at test.Main.main(Main.java:329)
Caused by: java.lang.NullPointerException
at com.sun.org.apache.xml.internal.serializer.SerializerBase.patchName(SerializerBase.java:271)
at com.sun.org.apache.xml.internal.serializer.SerializerBase.addAttribute(SerializerBase.java:429)
at com.sun.org.apache.xml.internal.serializer.ToSAXHandler.addUniqueAttribute(ToSAXHandler.java:438)
at xmlname.template$dot$0()
at xmlname.applyTemplates()
at xmlname.transform()
at com.sun.org.apache.xalan.internal.xsltc.runtime.AbstractTranslet.transform(AbstractTranslet.java:611)
at com.sun.org.apache.xalan.internal.xsltc.trax.TransformerImpl.transform(TransformerImpl.java:729)
... 4 more
---------
java.lang.NullPointerException
at com.sun.org.apache.xml.internal.serializer.SerializerBase.patchName(SerializerBase.java:271)
at com.sun.org.apache.xml.internal.serializer.SerializerBase.addAttribute(SerializerBase.java:429)
at com.sun.org.apache.xml.internal.serializer.ToSAXHandler.addUniqueAttribute(ToSAXHandler.java:438)
at xmlname.template$dot$0()
at xmlname.applyTemplates()
at xmlname.transform()
at com.sun.org.apache.xalan.internal.xsltc.runtime.AbstractTranslet.transform(AbstractTranslet.java:611)
at com.sun.org.apache.xalan.internal.xsltc.trax.TransformerImpl.transform(TransformerImpl.java:729)
at com.sun.org.apache.xalan.internal.xsltc.trax.TransformerImpl.transform(TransformerImpl.java:340)
at test.Main.XSLT(Main.java:393)
at test.Main.test(Main.java:342)
at test.Main.main(Main.java:329)

更新2

让我们用一个共同的来源进行测试。

test.xml

<?xml version="1.0" encoding="UTF-8"?>
<tests>
<test>
<text>Hi!</text>
</test>
</tests>

test.xsl

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

<xsl:output
method="html"
version="1.0"
encoding="utf-8"
indent="yes"
omit-xml-declaration="no"
media-type="application/xml"
doctype-public="-//W3C//DTD XHTML 1.0 Strict//EN"
doctype-system="http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"
/>

<xsl:template match="/">
<html xmlns="http://www.w3.org/1999/xhtml" lang="pt-BR" xml:lang="pt-BR">
<head>
<meta charset="UTF-8" />
<title>Test</title>
</head>
<body>
<h1>Test</h1>
<xsl:apply-templates/>
</body>
</html>
</xsl:template>

<xsl:template match="/tests/test">
<p><xsl:value-of select="text"/></p>
</xsl:template>

</xsl:stylesheet>

使用 StreamResult 给出以下输出。

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html lang="pt-BR" xml:lang="pt-BR" xmlns="http://www.w3.org/1999/xhtml">
<head>
<META http-equiv="Content-Type" content="text/html; charset=utf-8">
<meta charset="UTF-8">
<title>Test</title>
</head>
<body>
<h1>Test</h1>

<p>Hi!</p>

</body>
</html>

已解决

在适当的地方(在答案中!)查看我的答案

最佳答案

DOMResult output = new DOMResult();
TransformerFactory.newInstance().
newTransformer(new StreamSource(xsl)).
transform(input, output);
return (Document) output.getNode();

引用:
DOMResult#getNode()

Get the node that will contain the result DOM tree. If no node was set via DOMResult(Node node), ... , then the node will be set by the transformation, and may be obtained from this method once the transformation is complete.

关于java - 使用 DOMResult 进行 XML 转换,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17495803/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com