1200字范文,内容丰富有趣,写作的好帮手!
1200字范文 > JAVA xml转dom_如何在Java中将String转换为DOMSource?

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

时间:2023-02-19 11:11:44

相关推荐

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

我需要一些帮助.在我的String filedata变量中,我存储了一个

XMLdocument.现在我想将此变量转换为DOMSource类型并使用此代码:

DocumentBuilder db = DocumentBuilderFactory.newInstance().newDocumentBuilder();

Document doc = db.parse( new InputSource( new StringReader( filedata ) ) );

DOMSource source = new DOMSource(doc);

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

Transformer transformer = XMLTransformerFactory.getTransformer(messageType);

StreamResult res = new StreamResult(flatXML);

transformer.transform(source,res);

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

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

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

DOMSource source = new DOMSource(doc);

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

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。