001package armyc2.c2sd.renderer.utilities;
002
003import java.io.IOException;
004import java.io.StringReader;
005
006import javax.xml.parsers.DocumentBuilder;
007import javax.xml.parsers.DocumentBuilderFactory;
008import javax.xml.parsers.ParserConfigurationException;
009
010import org.w3c.dom.Document;
011import org.w3c.dom.Element;
012import org.w3c.dom.Node;
013import org.w3c.dom.NodeList;
014import org.xml.sax.InputSource;
015import org.xml.sax.SAXException;
016
017import android.util.Log;
018
019public class XMLParser {
020
021          /**
022           * from www.androidhive.info/2011/11/android-xml-parsing-tutorial/
023           * @param xml
024           * @return
025           */
026          public static Document getDomElement(String xml)
027          {
028                  Document doc = null;
029                  DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
030                  try
031                  {
032                          DocumentBuilder db = dbf.newDocumentBuilder();
033                          InputSource is = new InputSource();
034                          is.setCharacterStream(new StringReader(xml));
035                          doc = db.parse(is);
036                  }
037                  catch(ParserConfigurationException e)
038                  {
039                          Log.e("Error: ", e.getMessage());
040                          return null;
041                  }
042                  catch(SAXException e)
043                  {
044                          Log.e("Error: ", e.getMessage());
045                          return null;
046                  }
047                  catch(IOException e)
048                  {
049                          Log.e("Error: ", e.getMessage());
050                          return null;
051                  }
052                  return doc;
053          }
054          
055          public static final String getElementValue(Node elem)
056          {
057                  Node child;
058                  if(elem != null)
059                          if(elem.hasChildNodes()){
060                                  for(child = elem.getFirstChild(); child != null; child = child.getNextSibling()){
061                                          if(child.getNodeType() == Node.TEXT_NODE){
062                                                  return child.getNodeValue();
063                                          }
064                                  }
065                          }
066                  return "";
067          }
068          
069          public static final String getAttributeValue(Node node, String str)
070          {
071                  Element elem = null; 
072                  if(node != null)
073                          elem = (Element)node;
074                          if(elem.hasAttribute(str)){
075                                  return elem.getAttribute(str);
076                          }
077                  return "";
078          }
079          
080          public static String getValue(Element item, String str)
081          {
082                  NodeList n = item.getElementsByTagName(str);
083                  return getElementValue(n.item(0));
084          }
085          
086          /**
087           * Get the attribute from an element.  If not present returns ""
088           * @param elem
089           * @param attribName
090           * @return
091           */
092          public static String getAttribute(Element elem, String attribName)
093          {
094                  if(elem != null && elem.hasAttribute(attribName))
095                          return elem.getAttribute(attribName);
096                  else
097                          return "";
098          }
099}