Java XML Parse String parseXMLResponse(String xmlStr)

Here you can find the source of parseXMLResponse(String xmlStr)

Description

Returns a Document(DOM) object representing the parsed XML.

License

Open Source License

Parameter

Parameter Description
xmlStr a parameter

Return

Document

Declaration

public static Document parseXMLResponse(String xmlStr) 

Method Source Code

//package com.java2s;
/**/*from w w w  .j  a v  a  2 s .  c o m*/
 * Copyright 2007-2016, Kaazing Corporation. All rights reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import java.io.ByteArrayInputStream;
import java.io.IOException;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import org.w3c.dom.Document;
import org.xml.sax.SAXException;

public class Main {
    /**
     * Returns a Document(DOM) object representing the parsed XML.
     *
     * @param xmlStr
     * @return Document
     */
    public static Document parseXMLResponse(String xmlStr) {
        if ((xmlStr == null) || (xmlStr.length() == 0)) {
            return null;
        }

        // FIXME: error handling for the DOM parsing...
        try {
            DocumentBuilder db = DocumentBuilderFactory.newInstance()
                    .newDocumentBuilder();
            return db.parse(new ByteArrayInputStream(xmlStr.getBytes()));
        } catch (ParserConfigurationException | IOException | SAXException pcex) {
            //ignore
        }

        return null;
    }
}

Related

  1. parseXml(String xml)
  2. parseXml(String xml)
  3. parseXML(String xml)
  4. parseXML(String xml)
  5. parseXml(String xmlString)
  6. parseXMLString(final String xml)