解析 xml 响应 [英] Parsing xml response
问题描述
我有一个 JAVA 应用程序,我在其中发送一些 xml 请求并接收 xml 响应.我首先以字符串形式接收响应,然后写入一个文件并将该文件存储到文件系统中.然后在解析 xml 响应文件时,我从文件系统访问它,并将一些数据用于进一步的业务逻辑.
I have a JAVA application where I am sending some xml requests and receiving xml responses. I first receive response in string and then write a file and storing this file into file system. Then while parsing the xml response file I am accessing this from file system and use some of the data for further business logic.
File file = new File("log\\XMLMessage\\LastXMLResponse.xml");
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
Document doc = db.parse(file);
现在我正在考虑通过 Java Web Start (JWS) 应用程序分发这个 JAVA 应用程序,我知道我不能将此文件保存到 jar 文件中,因为此文件会定期进行修改.
Now I am thinking to distribute this JAVA application via Java Web Start (JWS) application and as I know I cannot keep this file into jar file since there will be modification in this file on regular basis.
你建议我做什么?我可以直接解析字符串吗(不需要将响应存储到文件中)?
What do you suggest me to do? Can I parse the String directly (no need to store the response into file)?
Document doc = db.parse(xmlMessage);
或者我可以在哪里保存这个文件?我不想向我的应用程序的用户显示此文件.
or where can I keep this file? I don't want to show this file to the user of my application.
推荐答案
可以,直接解析字符串即可,无需保存在文件中.
Yes, you can parse the string directly,no need to store it in a file.
试试这个:
String xml = "<xml></xml>";
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
Document doc = db.parse(new ByteArrayInputStream(xml.getBytes()));
System.out.println(doc);
这篇关于解析 xml 响应的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!