Here you can find the source of parseXML(String xml)
Parameter | Description |
---|---|
xml | a parameter |
public static Document parseXML(String xml) throws SAXException
//package com.java2s; import java.io.ByteArrayInputStream; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; 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 { /**// ww w . j a v a 2s . c om * Parses the XML passed in as an argument and returns a Document object * that represents the parsed XML * * @param xml * @return */ public static Document parseXML(String xml) throws SAXException { DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); try { // Using factory get an instance of document builder DocumentBuilder db = dbf.newDocumentBuilder(); // parse using builder to get DOM representation of the XML file Document domDoc = db.parse(new ByteArrayInputStream(xml.getBytes())); domDoc.normalize(); return domDoc; } catch (ParserConfigurationException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } // default for exceptions return null; } /** * Parses the XML stored in the given file and returns a Document object * that represents the parsed XML file * * @param xml * @return */ public static Document parseXML(File file) { DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); try { // Using factory get an instance of document builder DocumentBuilder db = dbf.newDocumentBuilder(); // parse using builder to get DOM representation of the XML file FileInputStream fis = new FileInputStream(file); Document domDoc = db.parse(fis); domDoc.normalize(); fis.close(); return domDoc; } catch (ParserConfigurationException e) { e.printStackTrace(); } catch (SAXException e) { e.printStackTrace(); } catch (FileNotFoundException e) { System.err.println("File not found: " + e.getMessage()); } catch (IOException e) { e.printStackTrace(); } // default for exceptions return null; } }