<a><b>不好</b>你好</a>目的:“你好”
“你好”,“不好”都是变化的,不能用replace替换掉。现在//a/text()会把“你好”跟“不好”都提取出来了

解决方案 »

  1.   

    //a/text()[1],听说是这样子,但是测试没通过
      

  2.   

    你说的是这样?//package com.ricky.www;/*
     <a><b>不好</b>你好</a>
    */
    import java.util.regex.Matcher;
    import java.util.regex.Pattern;public class Test{
    public static void main(String[] args){
    String content = "<a><b>不好</b>你好</a>";
    String regex = "<b>([^<]*)</b>";
    Pattern pattern = Pattern.compile(regex);
    Matcher matcher = pattern.matcher(content); if(matcher.find()){
    System.out.println(matcher.group(1));
    }
    }
    }
      

  3.   


    import java.io.IOException;
    import java.util.ArrayList;
    import java.util.Iterator;
    import java.util.List;import org.dom4j.Attribute;
    import org.dom4j.Document;
    import org.dom4j.DocumentException;
    import org.dom4j.Element;
    import org.dom4j.Text;
    import org.dom4j.io.SAXReader; 
    public class ff {
     public static void main(String[] args){

             String xmlName ="e://4455.xml";
             // 定义需要返回的第一级菜单的名字集合  
             List firstNames = new ArrayList();  
             // Attribute的属性集合  
             List attrs = new ArrayList();  
             // 声明SAXReader  
             SAXReader saxReader = new SAXReader();  
             try {  
                 Document doc = saxReader.read(xmlName);  
                 // 获得所有grade=1的Element的text的值  
                 String xpath = "/a";  
                 List list = doc.selectNodes(xpath);  
                 if(list.isEmpty())
                 {
                  System.out.print("111");
                 }
                 else
                 {
                  System.out.print("222");
                 }
                 
                 Iterator it = list.iterator();  
                 while (it.hasNext()) {  
                     Element elt = (Element) it.next();  
                    
                     System.out.print(elt.getText());
                 }  
       
             } catch (DocumentException e) {  
                 e.printStackTrace();  
             }  
            
      
      }
    }
    XML
    <?xml version="1.0" encoding="UTF-8"?>
    <a>
    <b>不好</b>
    你好
    </a>
      

  4.   

            try {
                String xml = "<a><b>不好</b>你好</a>";            XPathFactory factory = XPathFactory.newInstance();
                XPath xpath = factory.newXPath();            String result = xpath.evaluate("/a/text()", new InputSource(new StringReader(xml)));
                System.out.println(result);
            } catch (XPathExpressionException e) {
                e.printStackTrace();
            }
    输出:
    你好
    java 6u21