Guest User

Untitled

a guest
Jan 23rd, 2018
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.55 KB | None | 0 0
  1. import java.io.IOException;
  2. import java.util.Iterator;
  3. import java.util.regex.Matcher;
  4. import java.util.regex.Pattern;
  5.  
  6. import javax.xml.parsers.DocumentBuilder;
  7. import javax.xml.parsers.DocumentBuilderFactory;
  8. import javax.xml.parsers.ParserConfigurationException;
  9.  
  10. import org.apache.tools.ant.filters.StringInputStream;
  11. import org.w3c.dom.Document;
  12. import org.w3c.dom.Element;
  13. import org.w3c.dom.NodeList;
  14. import org.xml.sax.SAXException;
  15.  
  16. public class XMLParserUtil {
  17. public static String findFirstTagContent(String response, String tag) {
  18. return getFirstElement(response,tag).getTextContent();
  19. }
  20.  
  21. public static Element getFirstElement(String response,String tag) {
  22. NodeList nl = buildDocument(response).getElementsByTagName(tag);
  23. if(nl.getLength() >0 ){
  24. return (Element) nl.item(0);
  25. }
  26. return null;
  27. }
  28.  
  29. public static Element getLastElement(Document dom,String tag){
  30. NodeList nl = dom.getElementsByTagName(tag);
  31.  
  32. int size = nl.getLength();
  33. if (size == 0) {
  34. return null;
  35. }
  36.  
  37. return (Element) nl.item(size - 1);
  38. }
  39.  
  40. public static Iterator<Element> getElements(String xml,String tag){
  41. return getElements(buildDocument(xml),tag);
  42. }
  43.  
  44. public static Iterator<Element> getElements(Document dom,String tag){
  45. final NodeList nl = dom.getElementsByTagName(tag);
  46. return new Iterator<Element>(){
  47. private int index = 0;
  48. public boolean hasNext() {
  49. return index < nl.getLength();
  50. }
  51.  
  52. public Element next() {
  53. Element el = (Element) nl.item(index);
  54. index++;
  55. return el;
  56. }
  57.  
  58. public void remove() {
  59. throw new UnsupportedOperationException("unsupported");
  60. }
  61.  
  62. };
  63. }
  64.  
  65. public static Element getFirstElement(Element parent,String tag) {
  66. NodeList nl = parent.getElementsByTagName(tag);
  67. if(nl.getLength() >0 ){
  68. return (Element) nl.item(0);
  69. }
  70. return null;
  71. }
  72.  
  73. public static Document buildDocument(String response) {
  74. // get the factory
  75. DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
  76. try {
  77. // Using factory get an instance of document builder
  78. DocumentBuilder db = dbf.newDocumentBuilder();
  79. // parse using builder to get DOM representation of the XML file
  80. Document dom = db.parse(new StringInputStream(response));
  81. return dom;
  82.  
  83. } catch (ParserConfigurationException pce) {
  84. throw new IllegalArgumentException("Invalid xml input[" + response
  85. + "]", pce);
  86. } catch (SAXException se) {
  87. throw new IllegalArgumentException("Invalid xml input[" + response
  88. + "]", se);
  89. } catch (IOException ioe) {
  90. throw new IllegalArgumentException("Invalid xml input[" + response
  91. + "]", ioe);
  92. }
  93. }
  94.  
  95.  
  96. }
Add Comment
Please, Sign In to add comment