日期:2014-05-20  浏览次数:20761 次

关于使用jdom解析xml的一个问题,麻烦各位GGJJ帮忙看一下,谢谢!!!
<?xml   version= "1.0 "   encoding= "UTF-8 "?>
<books>
      <book   email= "zhoujunhui ">
          <name> eng <B> hello </B> lish </name>
          <price> 60.0 </price>
    </book>
</books>

现有Element   name  
如果用name.getText()我只能取到english
有什么办法能取到enghellolish吗
请大家帮忙知道一下


------解决方案--------------------
把你的XML改一下
<?xml version= "1.0 " encoding= "UTF-8 "?>
<books>
<book email= "zhoujunhui ">
<name>
<A> eng </A>
<B> hello </B>
<C> lish </C>
</name>
<price> 60.0 </price>
</book>
</books>
用下面的程序就能出现你的效果了
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Iterator;
import java.util.List;
//下面是引用到JDOM中的类
import org.jdom.Document;
import org.jdom.Element;
import org.jdom.JDOMException;
import org.jdom.input.SAXBuilder;
import org.jdom.output.XMLOutputter;

public class JDomParse {
public JDomParse() {
String xmlpath = "d:\\my.xml ";
SAXBuilder builder = new SAXBuilder(false);
try {
Document doc = builder.build(xmlpath);
Element books = doc.getRootElement();
List booklist = books.getChildren( "book ");
for (Iterator iter = booklist.iterator(); iter.hasNext();) {
Element book = (Element) iter.next();
String A=book.getChild( "name ").getChildText( "A ");
System.out.println(A);
String B=book.getChild( "name ").getChildText( "B ");
System.out.println(B);
String C=book.getChild( "name ").getChildText( "C ");
System.out.println(C);
}
XMLOutputter outputter = new XMLOutputter();
outputter.output(doc, new FileOutputStream(xmlpath));
} catch (JDOMException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}

public static void main(String[] args) {
new JDomParse();
}
}
------解决方案--------------------
name.getValue()