Here you can find the source of parseXMLDocument(String xmlDoc)
Parameter | Description |
---|---|
xmlDoc | XML document as a String. |
Parameter | Description |
---|---|
IllegalArgumentException | If the XML is malformed. |
public static Element parseXMLDocument(String xmlDoc) throws IllegalArgumentException
//package com.java2s; /*/*w w w . ja v a 2 s . co m*/ * Copyright (C) 2014 Dell, Inc. * * 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.Reader; import java.io.StringReader; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; import org.w3c.dom.Document; import org.w3c.dom.Element; import org.xml.sax.InputSource; public class Main { /** * Parse the given XML document, creating a DOM tree whose root Document object is * returned. An IllegalArgumentException is thrown if the XML is malformed. * * @param xmlDoc XML document as a String. * @return Root document element of the parsed DOM tree. * @throws IllegalArgumentException If the XML is malformed. */ public static Element parseXMLDocument(String xmlDoc) throws IllegalArgumentException { // Parse the given XML document returning its root document Element if it parses. // Wrap the document payload as an InputSource. Reader stringReader = new StringReader(xmlDoc); InputSource inputSource = new InputSource(stringReader); // Parse the document into a DOM tree. DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); DocumentBuilder parser = null; Document doc = null; try { parser = dbf.newDocumentBuilder(); doc = parser.parse(inputSource); } catch (Exception ex) { // Turn ParserConfigurationException, SAXException, etc. into an IllegalArgumentException throw new IllegalArgumentException("Error parsing XML document: " + ex.getMessage()); } return doc.getDocumentElement(); } /** * Parse an XML document from the given Reader, creating a DOM tree whose root * Document object is returned. An IllegalArgumentException is thrown if the XML is * malformed. * * @param reader Reader from which XML text is read. * @return Root document element of the parsed DOM tree. * @throws IllegalArgumentException If the XML is malformed. */ public static Element parseXMLDocument(Reader reader) throws IllegalArgumentException { // Wrap the document payload as an InputSource. InputSource inputSource = new InputSource(reader); // Parse the document into a DOM tree. DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); DocumentBuilder parser = null; Document doc = null; try { parser = dbf.newDocumentBuilder(); doc = parser.parse(inputSource); } catch (Exception ex) { // Turn ParserConfigurationException, SAXException, etc. into an IllegalArgumentException throw new IllegalArgumentException("Error parsing XML document: " + ex.getMessage()); } return doc.getDocumentElement(); } }