Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
javax.xml.transform.Source的字符串操作_Java_Xml_Xslt - Fatal编程技术网

javax.xml.transform.Source的字符串操作

javax.xml.transform.Source的字符串操作,java,xml,xslt,Java,Xml,Xslt,我使用Java和XSL样式表从XML文件中检索值并将其输出到文本文件 以下是使用的程序: TransformerFactory factory = TransformerFactory.newInstance(); Source xslt = new StreamSource(new File("transform.xsl")); Transformer transformer = factory.newTransformer(xslt); Source text

我使用Java和XSL样式表从XML文件中检索值并将其输出到文本文件

以下是使用的程序:

    TransformerFactory factory = TransformerFactory.newInstance();
    Source xslt = new StreamSource(new File("transform.xsl"));
    Transformer transformer = factory.newTransformer(xslt);
    Source text = new StreamSource(new File("inputXML.txt"));        
    transformer.transform(text, new StreamResult(new File("output.txt"))) ;
但最近我发现我将要读取的XML文件将有两个根节点,而不是一个。因此,我正在考虑执行字符串操作,以编程方式添加我自己的根节点,从而避免以下错误:

错误:“根元素后面的文档中的标记必须 格式正确。'错误: 'com.sun.org.apache.xml.internal.utils.WrappedRuntimeException: 必须删除根元素后面文档中的标记 结构合理。”

但我无法在javax.xml.transform.Source上执行任何字符串操作(强制转换不起作用)。 我不想使用中间文件来添加我的根节点,因为我担心这将证明代价高昂,因为我需要处理接近50k的XML记录。

有几个构造函数

Path inputPath = Paths.get("inputXML.txt");
String input = new String(Files.readAllBytes(inputPath,
                   StandardCharsets.UTF_8));
input = input.replaceFirst("<quasiroot", "<root>$0")
    + "</root>";

Source text = new StreamSource(new StringReader(input));        
Path inputPath=Path.get(“inputXML.txt”);
字符串输入=新字符串(Files.readAllBytes(inputPath,
标准字符集(UTF_8));

input=input.replaceFirst(“注意,在Java世界中,您有Xerces之类的XML解析器,支持,因此您可以简单地构造一个引用其他文件的文件,例如

<!DOCTYPE root [
  <!ENTITY input SYSTEM "inputXML.txt">
]>
<root>&input;</root>

&输入;
然后,您所需要做的就是加载该文件作为XSLT的源。不需要进行字符串操作,至少不需要操作整个XML,如果需要,您可以直接将上述内容构造为字符串,并通过StringReader将其传递给StreamSource,在StringReader中将系统id设置为输入XML的目录:

    String input = "inputXML.txt";
    File dir = new File(".");
    String baseUri = dir.toURI().toASCIIString();
    String inputXml = "<!DOCTYPE root [  <!ENTITY input SYSTEM \"" + input + "\">]><root>&input;</root>";
    TransformerFactory factory = TransformerFactory.newInstance();
    Source xslt = new StreamSource(new File("transform.xsl"));
    Transformer transformer = factory.newTransformer(xslt);
    Source text = new StreamSource(new StringReader(inputXml), baseUri);
    transformer.transform(text, new StreamResult(new File("output.txt")));
String input=“inputXML.txt”;
文件目录=新文件(“.”);
字符串baseUri=dir.toURI().toASCIIString();
字符串inputXml=“&input;”;
TransformerFactory=TransformerFactory.newInstance();
Source xslt=newstreamsource(新文件(“transform.xsl”);
变压器=工厂新变压器(xslt);
源文本=新的StreamSource(新的StringReader(inputXml),baseUri);
transform(文本,新的StreamResult(新文件(“output.txt”));

我使用了“输入”处理方式的细微变化来获得结果。谢谢。非常感谢