Get the tools you need to learn Java skills fast!
Video tutorials, eBooks, hands-on lab exercises, sample code.
Get started
The moose likes I/O and Streams and the fly likes Unable to parse an xml string using builder.parse Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of The Software Craftsman this week in the Agile forum!
JavaRanch » Java Forums » Java » I/O and Streams
Bookmark "Unable to parse an xml string using builder.parse " Watch "Unable to parse an xml string using builder.parse " New topic

Unable to parse an xml string using builder.parse

Praveen Shivashankar

Joined: Jul 20, 2008
Posts: 13

I am trying to take an xml string, convert that into a document type to be used for a function that is expected to verify the validity of the contents of this xml string. However, after converting to a document type, the builder.parse() attribute doesn't seem to return any values at all, due to which the validation function is failing. Given below is a code snippet for this.

import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;

String xmlResponse = "<?xml version="1.0" encoding="UTF-8" standalone="no"?><AuthRes code="5ee2f1d4-4cd6-4996-abad-9a2bc64847a9" info="01a8e6dc0e0c7eb69fb2897d2065284f578bcdfd6e18000000" ret="y" txn="DefaultTXN"><Signature xmlns=""><SignedInfo><CanonicalizationMethod Algorithm=""/><SignatureMethod Algorithm=""/><Reference URI=""><Transforms><Transform Algorithm=""/></Transforms><DigestMethod Algorithm=""/><DigestValue>K2NuXDP3eFgzglLeHCD6tpogEvs=</DigestValue></Reference></SignedInfo><SignatureValue>m8XsACzxcFPxHRGqkD8tBzKTLlo9azB2e+4+RJ2c4sVettq6QymcVN1BIx0PYfu/lUDxxNlGHsWjcODcrdWnSlYff/6k8sBaTTAkq8+RTATokAOP9LRMgbszfCJLQx1Or+p8EUaeI5J+jEHe+pGFh6b/lsttX6p69lWeq6bp8oI=</SignatureValue></Signature></AuthRes>"

DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
Document resDocument = builder.parse(new InputSource(new StringReader(responseBody)));

System.out.println("The Signature is certified properly");

I like talking to myself, coz I always hear what I like!
William Brogden
Author and all-around good cowpoke

Joined: Mar 22, 2000
Posts: 12913
Surely that is not the real code building that String, with all those quote characters mixed in.

Why are you so sure a Document is being created? Where are the statements to catch ard report exceptions?

It is sorta covered in the JavaRanch Style Guide.
subject: Unable to parse an xml string using builder.parse