View Javadoc
1   package org.kuali.ole.docstore.common.util;
2   
3   import org.apache.commons.lang3.StringEscapeUtils;
4   import org.w3c.dom.Document;
5   import org.w3c.dom.NamedNodeMap;
6   import org.w3c.dom.Node;
7   import org.w3c.dom.NodeList;
8   import org.xml.sax.InputSource;
9   import org.xml.sax.SAXException;
10  
11  import javax.xml.parsers.DocumentBuilder;
12  import javax.xml.parsers.DocumentBuilderFactory;
13  import javax.xml.parsers.ParserConfigurationException;
14  import javax.xml.transform.OutputKeys;
15  import javax.xml.transform.Transformer;
16  import javax.xml.transform.TransformerException;
17  import javax.xml.transform.TransformerFactory;
18  import javax.xml.transform.dom.DOMSource;
19  import javax.xml.transform.stream.StreamResult;
20  import java.io.*;
21  
22  /**
23   * Created with IntelliJ IDEA.
24   * User: jayabharathreddy
25   * Date: 1/8/14
26   * Time: 1:37 PM
27   * To change this template use File | Settings | File Templates.
28   */
29  public class ParseXml {
30  
31  
32      public static Node getNode(String tagName, NodeList nodes) {
33          for (int x = 0; x < nodes.getLength(); x++) {
34              Node node = nodes.item(x);
35              if (node.getNodeName().equalsIgnoreCase(tagName)) {
36                  return node;
37              }
38          }
39  
40          return null;
41      }
42  
43      public static Node getNode(String tagName, NodeList nodes,int i) {
44          int k=0;
45          for (int x = 0; x < nodes.getLength(); x++) {
46              Node node = nodes.item(x);
47              if (node.getNodeName().equalsIgnoreCase(tagName)) {
48                  if(k==i){
49                  return node;
50                  }
51                  k++;
52              }
53          }
54  
55          return null;
56      }
57  
58      public static Node getNodeXml(NodeList nodes,int i) {
59         return nodes.item(i);
60      }
61  
62      public static String getNodeValue(Node node) {
63          NodeList childNodes = node.getChildNodes();
64          for (int x = 0; x < childNodes.getLength(); x++) {
65              Node data = childNodes.item(x);
66              if (data.getNodeType() == Node.TEXT_NODE)
67                  return data.getNodeValue();
68          }
69          return "";
70      }
71  
72      public static String getNodeValue(String tagName, NodeList nodes) {
73          for (int x = 0; x < nodes.getLength(); x++) {
74              Node node = nodes.item(x);
75              if (node.getNodeName().equalsIgnoreCase(tagName)) {
76                  NodeList childNodes = node.getChildNodes();
77                  for (int y = 0; y < childNodes.getLength(); y++) {
78                      Node data = childNodes.item(y);
79                      if (data.getNodeType() == Node.TEXT_NODE)
80                          return data.getNodeValue();
81                  }
82              }
83          }
84          return "";
85      }
86  
87      public static String getNodeAttr(String attrName, Node node) {
88          NamedNodeMap attrs = node.getAttributes();
89          for (int y = 0; y < attrs.getLength(); y++) {
90              Node attr = attrs.item(y);
91              if (attr.getNodeName().equalsIgnoreCase(attrName)) {
92                  return attr.getNodeValue();
93              }
94          }
95          return "";
96      }
97  
98      public static String getNodeAttr(String tagName, String attrName, NodeList nodes) {
99          for (int x = 0; x < nodes.getLength(); x++) {
100             Node node = nodes.item(x);
101             if (node.getNodeName().equalsIgnoreCase(tagName)) {
102                 NodeList childNodes = node.getChildNodes();
103                 for (int y = 0; y < childNodes.getLength(); y++) {
104                     Node data = childNodes.item(y);
105                     if (data.getNodeType() == Node.ATTRIBUTE_NODE) {
106                         if (data.getNodeName().equalsIgnoreCase(attrName))
107                             return data.getNodeValue();
108                     }
109                 }
110             }
111         }
112 
113         return "";
114     }
115 
116     public static String nodeToString(Node node) {
117         StringWriter sw = new StringWriter();
118         try {
119             Transformer t = TransformerFactory.newInstance().newTransformer();
120             t.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
121             t.setOutputProperty(OutputKeys.INDENT, "yes");
122             t.transform(new DOMSource(node), new StreamResult(sw));
123         } catch (TransformerException te) {
124             System.out.println("nodeToString Transformer Exception");
125         }
126         return sw.toString();
127     }
128 
129     public static String formatXml(String xml) {
130         StringWriter sw = new StringWriter();
131         try {
132             DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
133             DocumentBuilder builder = null;
134             builder = factory.newDocumentBuilder();
135             Document doc = null;
136             doc = builder.parse(new InputSource(new StringReader(xml)));
137             Node root = doc.getFirstChild();
138             Transformer t = TransformerFactory.newInstance().newTransformer();
139             t.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
140             t.setOutputProperty(OutputKeys.INDENT, "yes");
141             t.transform(new DOMSource(root), new StreamResult(sw));
142         } catch (Exception te) {
143             System.out.println("nodeToString Transformer Exception");
144         }
145         return sw.toString();
146     }
147 
148 
149     public static String getEscapedContent(String xml){
150         int startIndex= xml.indexOf("<content>")+9;
151         int endIndex= xml.indexOf("</content>");
152         String contentString= xml.substring(startIndex,endIndex);
153         String escape = StringEscapeUtils.escapeXml(contentString) ;
154         return xml.replace(contentString,escape);
155     }
156 }