Parsing xml response

本秂侑毒 提交于 2019-12-25 03:53:37

问题


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);

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.


回答1:


Take the String, make a StringReader from String, make a InputSource from StringReader, then call parse on your DocumentBuilder.




回答2:


Yes, you can parse the string directly,no need to store it in a file.

Try this:

String xml = "<xml></xml>";
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
Document doc = db.parse(new ByteArrayInputStream(xml.getBytes()));
System.out.println(doc);


来源:https://stackoverflow.com/questions/6612911/parsing-xml-response

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!