Here you can find the source of parseXMLString(final String xml)
Parameter | Description |
---|---|
xml | String with XML content. |
Parameter | Description |
---|---|
Exception | on error. |
public static final Element parseXMLString(final String xml) throws Exception
//package com.java2s; /******************************************************************************* * Copyright (c) 2010 Oak Ridge National Laboratory. * All rights reserved. This program and the accompanying materials * are made available under the terms of the Eclipse Public License v1.0 * which accompanies this distribution, and is available at * http://www.eclipse.org/legal/epl-v10.html ******************************************************************************/ 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; import org.xml.sax.SAXException; import org.xml.sax.SAXParseException; public class Main { /** Reads a string that contains XML markup as a DOM. * @param xml String with XML content. * @return Returns the root Element. * @throws Exception on error.//from www . ja v a2 s . co m */ public static final Element parseXMLString(final String xml) throws Exception { Document doc; // Open the XML file, read as DOM try { DocumentBuilder docBuilder = DocumentBuilderFactory.newInstance().newDocumentBuilder(); Reader r = new StringReader(xml); InputSource s = new InputSource(r); doc = docBuilder.parse(s); } catch (SAXParseException err) { throw new Exception("Parse error in '" + xml + "', line " + err.getLineNumber() + ", uri " + err.getSystemId() + " " + err.getMessage()); } catch (SAXException e) { Exception x = e.getException(); throw (x == null) ? e : x; } doc.getDocumentElement().normalize(); return doc.getDocumentElement(); } }