如何在Java中将String转换为DOMSource?

时间:2022-04-11 15:54:27

I need some help. In my String filedata variable I stored an XMLdocument. Now I want to convert this variable to a DOMSource type and use this code:

我需要一些帮助。在我的String filedata变量中,我存储了一个XMLdocument。现在我想将此变量转换为DOMSource类型并使用此代码:

DocumentBuilder db = DocumentBuilderFactory.newInstance().newDocumentBuilder();
Document doc = db.parse( new InputSource( new StringReader( filedata ) ) ); 
DOMSource source = new DOMSource(doc);

and transform by javax.xml.transform.Transformer :

并通过javax.xml.transform.Transformer进行转换:

 Transformer transformer = XMLTransformerFactory.getTransformer(messageType);
 StreamResult res = new StreamResult(flatXML);
 transformer.transform(source, res);

But my flatXML is empty after transformation. I checked my doc variable, and it contains my XML document and parsed everything right. If I change my source to the real path everything is ok and works fine :

但我的flatXML在转换后是空的。我检查了我的doc变量,它包含我的XML文档并解析了所有内容。如果我将源代码更改为真实路径,一切正常并且工作正常:

 Source source = new StreamSource("c:\\temp\\log\\SMKFFcompleteProductionPlan.xml");

I think my problem situated in this line of code :

我认为我的问题位于这行代码中:

DOMSource source = new DOMSource(doc);

but I don't know how to solve this problem.

但我不知道如何解决这个问题。

2 个解决方案

#1


14  

Why are you trying to construct a DOMSource? If all you want is a source to supply as input to a transformation, it is much more efficient to supply a StreamSource, which you can do as

你为什么要构建一个DOMSource?如果您想要的只是提供转换输入的源,那么提供StreamSource会更有效率,您可以这样做

new StreamSource(new StringReader(fileData))

preferably supplying a systemId as well. Constructing the DOM is a waste of time.

最好也提供systemId。构建DOM是浪费时间。

#2


1  

FYI: There are no constructor of Class DOMSource having argument only String like DOMSource(String).
The constructors are as follows:
i)DOMSource()
ii)DOMSource(Node n)
iii)DOMSource(Node node, String systemID)
Please see : http://docs.oracle.com/javase/6/docs/api/javax/xml/transform/dom/DOMSource.html

仅供参考:没有类DOMSource的构造函数只有String参数,如DOMSource(String)。构造函数如下:i)DOMSource()ii)DOMSource(Node n)iii)DOMSource(Node node,String systemID)请参阅:http://docs.oracle.com/javase/6/docs/api/javax /xml/transform/dom/DOMSource.html

#1


14  

Why are you trying to construct a DOMSource? If all you want is a source to supply as input to a transformation, it is much more efficient to supply a StreamSource, which you can do as

你为什么要构建一个DOMSource?如果您想要的只是提供转换输入的源,那么提供StreamSource会更有效率,您可以这样做

new StreamSource(new StringReader(fileData))

preferably supplying a systemId as well. Constructing the DOM is a waste of time.

最好也提供systemId。构建DOM是浪费时间。

#2


1  

FYI: There are no constructor of Class DOMSource having argument only String like DOMSource(String).
The constructors are as follows:
i)DOMSource()
ii)DOMSource(Node n)
iii)DOMSource(Node node, String systemID)
Please see : http://docs.oracle.com/javase/6/docs/api/javax/xml/transform/dom/DOMSource.html

仅供参考:没有类DOMSource的构造函数只有String参数,如DOMSource(String)。构造函数如下:i)DOMSource()ii)DOMSource(Node n)iii)DOMSource(Node node,String systemID)请参阅:http://docs.oracle.com/javase/6/docs/api/javax /xml/transform/dom/DOMSource.html