1

I am trying to send XML file to one of my Servlet class and I am able to do that. Below is my XML file-

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<request id="a487bd863c3e4513a7893966f8e186f1">
<app hash="sha1"/>
</request>

And following is my Servlet doPost method in which I need to parse the XML file and get id and hash value from that XML file.

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        String line;
        BufferedReader br = new BufferedReader(request.getReader());
        while ((line = br.readLine()) != null) {
            System.out.println(line);
            // now here parse the line and get `id and hash value`
            // from it.
        }
    }

I am thinking what is the best way to get id and hash value from that XML file. I know one way is to parse the XML file and get the id and hash value. Is there any easy or direct way to get what I am looking for?

Any simple example will be appreciated.

AKIWEB
  • 19,008
  • 67
  • 180
  • 294

2 Answers2

2

try XPath

    XPathFactory xPathFactory = XPathFactory.newInstance();
    XPath xPath = xPathFactory.newXPath();
    String id = xPath.evaluate("/request/@id",
            new InputSource(request.getInputStream()));     

If you need both id and hash then

    InputStream is = request.getInputStream();
    Document doc = DocumentBuilderFactory.newInstance()
            .newDocumentBuilder().parse(is);
    String id = ((Element) doc.getElementsByTagName("request").item(0))
            .getAttribute("id");
    String hash = ((Element) doc.getElementsByTagName("app").item(0))
            .getAttribute("hash");      

since parsers close InpuStream we need to prevent it

    InputStream is = new BufferedInputStream(request.getInputStream()) {
        public void close() {
        }
    };
Visruth
  • 3,430
  • 35
  • 48
Evgeniy Dorofeev
  • 133,369
  • 30
  • 199
  • 275
  • Thanks Evgeniy for the suggestion. And what about hash? I need to pull that as well from the XML. – AKIWEB Jul 02 '13 at 04:56
  • If I am doing like this after String id line - `String hash = XPathFactory.newInstance().newXPath().evaluate("/app/@hash", new InputSource(request.getInputStream()));`. I am getting stream closed exception – AKIWEB Jul 02 '13 at 04:58
  • you have to read completely and buffer `request.getInputStream()` in memory or disk and then use it. Request stream over HTTP socket can't be read back again. – Bimalesh Jha Jul 02 '13 at 05:19
  • @BimaleshJha, Thanks for suggestion. Can you provide a simple example? – AKIWEB Jul 02 '13 at 05:21
  • @akirahin3 check this snippet: ByteArrayOutputStream bytes = new ByteArrayOutputStream(); //in memory, assuming small data IOUtils.copy(request.getInputStream, bytes); //using apache commons IOUtils //use InputSource on in memory buffer in XPath API new InputSource(new ByteArrayInputStream(bytes.toByteArray())); – Bimalesh Jha Jul 02 '13 at 05:35
1

You can use sax parser or dom parser to parse xml file.

Visruth
  • 3,430
  • 35
  • 48