0

I am trying to convert a samlResponse in string format to org.w3c.dom.Document to validate it. But it yields null even though I have used couple of different ways.
One way is as below: Here even the inputStream is null.
BasicParserPool bpp = new BasicParserPool(); bpp.setNamespaceAware(true); Document doc= bpp.parse(new ByteArrayInputStream(samlResponse.getBytes())); Element responseElement = doc.getDocumentElement();

where the string samlResponse is as below (just a snippet):

String samlResponse = "<saml2p:Response xmlns:saml2p=\"urn:oasis:names:tc:SAML:2.0:protocol\"  xmlns:xs=\"http://www.w3.org/2001/XMLSchema\" Version=\"2.0\">   <saml2:Issuer etc etc

Any thoughts where I am going wrong?

2
  • what, specifically, is "null"? Commented Sep 23, 2014 at 19:52
  • also, don't convert xml strings to bytes in order to parse as you can break the character encoding. use a StringReader instead. Commented Sep 23, 2014 at 19:52

2 Answers 2

2

BasicParserPool is an OpenSAML class, and I haven't used OpenSAML, so I can't say why it isn't working.

I can give you a simple alternative that works for me however.

I convert Strings to org.w3c.dom.Document using javax.xml.parsers.DocumentBuilderFactory and javax.xml.parsers.DocumentBuilder:

DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
dbf.setNamespaceAware(true);
Document result = dbf.newDocumentBuilder().parse(new ByteArrayInputStream(xml.getBytes()));

where "xml" is the String to convert. There is some exception-catching that I have left out.

The API is here: DocumentBuilder API

Hope this helps.

Sign up to request clarification or add additional context in comments.

Comments

1

Hope this piece of below code helps

import java.io.IOException;
import java.io.StringReader;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;

/**
 *
 * @author karthikeyan.s1
 */
public class Parser {

    public Document getDomElement(String xml) {
        Document doc = null;
        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
        dbf.setCoalescing(true);
        dbf.setNamespaceAware(true);
        try {

            DocumentBuilder db = dbf.newDocumentBuilder();
            InputSource is = new InputSource();
            is.setCharacterStream(new StringReader(xml));
            doc = db.parse(is);
        } catch (ParserConfigurationException e) {
            // Log.e("Error: ", e.getMessage());
            return null;
        } catch (SAXException e) {
            // Log.e("Error: ", e.getMessage());
            return null;
        } catch (IOException e) {
            //  Log.e("Error: ", e.getMessage());
            return null;
        }

        return doc;
    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.