【问题标题】:XML tags with regex in java [duplicate]java中带有正则表达式的XML标签[重复]
【发布时间】:2013-07-08 13:21:52
【问题描述】:

我想用正则表达式获取字符串中的所有 xml 标签。
示例:

<label>123</label>

我想得到:

<label></label>

【问题讨论】:

  • &lt;example/&gt;这样的元素呢?

标签: java xml regex pattern-matching


【解决方案1】:
    1. try to take the 2 example 

for a number use this regex
\d-->Any digit, short for [0-9]

    import org.w3c.dom.Document;
    import org.w3c.dom.Element;
    import org.w3c.dom.Node;
    import org.w3c.dom.NodeList;
    import org.xml.sax.SAXException;
    import org.xml.sax.SAXParseException;


          try {

                        DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance();
                        DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder();
                        Document doc = docBuilder.parse (new File("c:\\tmp\\my.xml"));

                        // normalize text representation
                        doc.getDocumentElement ().normalize ();
                        System.out.println ("Root element of the doc is " +  doc.getDocumentElement().getNodeName());


                        NodeList listOfBooks = doc.getElementsByTagName("book");
                        int totalBooks = listOfBooks.getLength();
                        System.out.println("Total no of books : " + totalBooks);

                        for(int i=0; i<listOfBooks.getLength() ; i++){


                            Node firstBookNode = listOfBooks.item(i);
                            if(firstBookNode.getNodeType() == Node.ELEMENT_NODE){


                                Element firstElement = (Element)firstBookNode;

                                System.out.println("Year :"+firstElement.getAttribute("year"));

                                //-------
                                NodeList firstNameList = firstElement.getElementsByTagName("title");
                                Element firstNameElement = (Element)firstNameList.item(0);

                                NodeList textFNList = firstNameElement.getChildNodes();
                                System.out.println("title : " + 
                                       ((Node)textFNList.item(0)).getNodeValue().trim());



                            }


                        }//end of for loop with s var


                    }catch (SAXParseException err) {
                    System.out.println ("** Parsing error" + ", line " 
                         + err.getLineNumber () + ", uri " + err.getSystemId ());
                    System.out.println(" " + err.getMessage ());

                    }catch (SAXException e) {
                    Exception x = e.getException ();
                    ((x == null) ? e : x).printStackTrace ();

                    }catch (Throwable t) {
                    t.printStackTrace ();
                    }


                }

///Regex example

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class RegexMatches
{
    public static void main( String args[] ){

      // String to be scanned to find the pattern.
      String line = "This order was places for QT3000! OK?";
      String pattern = "(.*)(\\d+)(.*)";

      // Create a Pattern object
      Pattern r = Pattern.compile(pattern);

      // Now create matcher object.
      Matcher m = r.matcher(line);
      if (m.find( )) {
         System.out.println("Found value: " + m.group(0) );
         System.out.println("Found value: " + m.group(1) );
         System.out.println("Found value: " + m.group(2) );
      } else {
         System.out.println("NO MATCH");
      }
   }
}

【讨论】:

    猜你喜欢
    • 2014-06-02
    • 2013-03-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-01-07
    相关资源
    最近更新 更多