Created
August 31, 2011 23:51
-
-
Save tony1223/1185073 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import java.io.IOException; | |
| import java.util.Iterator; | |
| import java.util.regex.Matcher; | |
| import java.util.regex.Pattern; | |
| import javax.xml.parsers.DocumentBuilder; | |
| import javax.xml.parsers.DocumentBuilderFactory; | |
| import javax.xml.parsers.ParserConfigurationException; | |
| import org.apache.tools.ant.filters.StringInputStream; | |
| import org.w3c.dom.Document; | |
| import org.w3c.dom.Element; | |
| import org.w3c.dom.NodeList; | |
| import org.xml.sax.SAXException; | |
| public class XMLParserUtil { | |
| public static String findFirstTagContent(String response, String tag) { | |
| return getFirstElement(response,tag).getTextContent(); | |
| } | |
| public static Element getFirstElement(String response,String tag) { | |
| NodeList nl = buildDocument(response).getElementsByTagName(tag); | |
| if(nl.getLength() >0 ){ | |
| return (Element) nl.item(0); | |
| } | |
| return null; | |
| } | |
| public static Element getLastElement(Document dom,String tag){ | |
| NodeList nl = dom.getElementsByTagName(tag); | |
| int size = nl.getLength(); | |
| if (size == 0) { | |
| return null; | |
| } | |
| return (Element) nl.item(size - 1); | |
| } | |
| public static Iterator<Element> getElements(String xml,String tag){ | |
| return getElements(buildDocument(xml),tag); | |
| } | |
| public static Iterator<Element> getElements(Document dom,String tag){ | |
| final NodeList nl = dom.getElementsByTagName(tag); | |
| return new Iterator<Element>(){ | |
| private int index = 0; | |
| public boolean hasNext() { | |
| return index < nl.getLength(); | |
| } | |
| public Element next() { | |
| Element el = (Element) nl.item(index); | |
| index++; | |
| return el; | |
| } | |
| public void remove() { | |
| throw new UnsupportedOperationException("unsupported"); | |
| } | |
| }; | |
| } | |
| public static Element getFirstElement(Element parent,String tag) { | |
| NodeList nl = parent.getElementsByTagName(tag); | |
| if(nl.getLength() >0 ){ | |
| return (Element) nl.item(0); | |
| } | |
| return null; | |
| } | |
| public static Document buildDocument(String response) { | |
| // get the factory | |
| 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 dom = db.parse(new StringInputStream(response)); | |
| return dom; | |
| } catch (ParserConfigurationException pce) { | |
| throw new IllegalArgumentException("Invalid xml input[" + response | |
| + "]", pce); | |
| } catch (SAXException se) { | |
| throw new IllegalArgumentException("Invalid xml input[" + response | |
| + "]", se); | |
| } catch (IOException ioe) { | |
| throw new IllegalArgumentException("Invalid xml input[" + response | |
| + "]", ioe); | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment