Saturday, December 31, 2011

Get Value For a XML Element


<< Previous Table of Categories Next >>


 /**
   * Feel free to copy and use this.
   * If you have any comments or better solutions, please paste on my blog: 
   * http://blog.yanmingyu.com or email me feedback.andrewyu@gmail.com
   *
   * This method uses Regular Expression to extract content of an XML element 
   * from a XML string
   *
   * @param xmlString
   * @param tagName
   * @return
   *
   */
  public static String getValueForXmlElement(String xmlString, String tagName)
  {
      String xmlTagValue = null;
      try
      {
        Pattern patter = Pattern.compile("(<|([A-Za-z0-9.]*:))" + tagName 
                                      + "(>|(\\s[\\sA-Za-z0-9./:\"=']*>))(.*?)</");
        Matcher matcher = patter.matcher(xmlString);
        while (matcher.find())
        {
          xmlTagValue = matcher.group(5);
        }
      }
      catch(Exception e){;}
      return xmlTagValue;
  } 
 

No comments:

Post a Comment