xml文件为
<?xml version="1.0" encoding="UTF-8"?>
<A>
<ONE single="true">
          <aa>aa1</aa>
          <bb>bb1</bb>
          <cc>cc1</cc>
        </ONE>
<TWO single="true">
          <aa>aa2</aa>
          <bb>bb2</bb>
          <cc>cc2</cc>
</TWO>
</A>现在需要些一个方法 getXml(str1,str2)
比如getXml("ONE","aa")返回aa1
getXml("ONE","bb")返回bb1
getXml("TWO","aa")返回bb2请教各位怎么写!

解决方案 »

  1.   

    这是我写的解析msn聊天记录的类,当初是做实验的,你看看吧package home.jinhf.parsemsn;import java.io.File;
    import java.io.IOException;
    import javax.xml.parsers.DocumentBuilder;
    import javax.xml.parsers.DocumentBuilderFactory;
    import javax.xml.parsers.ParserConfigurationException;import org.w3c.dom.Document;
    import org.w3c.dom.Element;
    import org.w3c.dom.NamedNodeMap;
    import org.w3c.dom.Node;
    import org.w3c.dom.NodeList;
    import org.xml.sax.SAXException;public class ParseMSN {

    public ParseMSN(String fileName){
    if(!fileName.substring(fileName.length() - 4).equals(".xml")){
    fileName = fileName + ".xml";
    }
    File file = new File(fileName);
    try {
    DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
    Document doc = builder.parse(file);
    parse(doc);
    } catch (ParserConfigurationException e) {
    e.printStackTrace();
    } catch (SAXException e) {
    e.printStackTrace();
    } catch (IOException e) {
    e.printStackTrace();
    } }

    public void parse(Document doc){
    Element el = doc.getDocumentElement();
    NodeList message = el.getElementsByTagName("Message");
    NodeList from = el.getElementsByTagName("From");
    NodeList to = el.getElementsByTagName("To");
    for(int i=0;i<message.getLength();i++){
    Node node_m = message.item(i);                        
    Node node_f = from.item(i).getFirstChild();
    Node node_t = to.item(i).getFirstChild();
    NamedNodeMap attirbutes_m = node_m.getAttributes();
    NamedNodeMap attirbutes_f = node_f.getAttributes();
    NamedNodeMap attirbutes_t = node_t.getAttributes();
    String info = "";
    info = info + attirbutes_m.getNamedItem("DateTime").getTextContent() + "  ";
    info = info + attirbutes_f.getNamedItem("FriendlyName").getTextContent() + " to ";
    info = info + attirbutes_t.getNamedItem("FriendlyName").getTextContent() + "  ";
    System.out.println(info + node_m.getTextContent());
    }
    }

    }
      

  2.   

    我先看看 对我有帮助的都给分;)
    问题中有写错
    getXml("TWO","aa")返回aa2
      

  3.   

    用我写的类吧ReadXml rx=new ReadXml("xml的物理路径");
    String str=rx.getNodeValue("ONE/aa");
    str即是你想要的!
      

  4.   

    或者你直接参考 jdom中 xpath的用法例子,
    我就是封装的它的那个例子
      

  5.   

    第一不是有dom4j现成的解析xml的了吗?为什么要自己写?
    第二用dom解析也容易啊,把xml读如成documengt用dom操作去你要的内容就行了,也是现成的方法.
    具体的看看API就解决了.楼主表偷懒,自己看书解决很有成就感的,具体例子我有写就不给你了:)
      

  6.   

    dom4j非常方便,不过就是方法多了些
      

  7.   

    要是用dom4j的话,会更简单一些。
    重载getXml()方法可以读取任意层次的叶子节点。
    第一个参数是最外层的根节点,依次类推。package com.mycompany.xmltest;import java.io.BufferedReader;
    import java.io.File;
    import java.io.FileNotFoundException;
    import java.io.FileReader;
    import java.io.IOException;
    import java.util.HashMap;import javax.xml.parsers.DocumentBuilder;
    import javax.xml.parsers.DocumentBuilderFactory;
    import javax.xml.parsers.ParserConfigurationException;import org.w3c.dom.Document;
    import org.w3c.dom.Node;
    import org.w3c.dom.NodeList;
    import org.xml.sax.InputSource;
    import org.xml.sax.SAXException;public class ARead { /*
     * Reload this method to get text contents within a leaf of any level
     * 
     * @Return null if there is no such a leaf
     */
    public Object getXml(String key1, String key2, String key3) {
    HashMap retMap = readXmlFile(new File("F:\\a.xml"));
    if (!retMap.isEmpty()) {
    Object obj = retMap.get(key1);
    if (obj instanceof HashMap) {
    retMap = (HashMap) obj;
    if (!retMap.isEmpty()) {
    obj = retMap.get(key2);
    if (obj instanceof HashMap) {
    retMap = (HashMap) obj;
    if (!retMap.isEmpty()) {
    return retMap.get(key3);
    }
    }
    }
    }
    }
    return null;
    } private HashMap readXmlFile(File thefile) {
    HashMap map = new HashMap(); BufferedReader bfreader = null;
    DocumentBuilder docbuilder = null;
    Document doc = null; try {
    bfreader = new BufferedReader(new FileReader(thefile));
    } catch (FileNotFoundException e) {
    e.printStackTrace();
    } DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    try {
    docbuilder = factory.newDocumentBuilder();
    } catch (ParserConfigurationException e) {
    e.printStackTrace();
    } try {
    doc = docbuilder.parse(new InputSource(bfreader));
    } catch (SAXException e) {
    e.printStackTrace();
    } catch (IOException e) {
    e.printStackTrace();
    } finally {
    try {
    bfreader.close();
    } catch (IOException e) {
    e.printStackTrace();
    }
    } if (doc.hasChildNodes()) {
    NodeList nlist_01 = doc.getChildNodes();
    parseChild(nlist_01, map);
    } return map;
    } /*
     * Recursive call
     */
    private void parseChild(NodeList nlist, HashMap themap) {
    for (int i = 0; i < nlist.getLength(); i++) {
    Node node = nlist.item(i);
    if (node.hasChildNodes()
    && node.getFirstChild().getNextSibling() != null) {
    HashMap sonMap = new HashMap();
    parseChild(node.getChildNodes(), sonMap);
    themap.put(node.getNodeName(), sonMap);
    } else {
    if (node.getTextContent() != null
    && !"".equals(node.getTextContent())) {
    themap.put(node.getNodeName(), node.getTextContent());
    }
    }
    } } public static void main(String[] args) {
    ARead test = new ARead();
    System.out.println(test.getXml("A", "ONE", "aa"));
    System.out.println(test.getXml("A", "ONE", "cc"));
    System.out.println(test.getXml("A", "TWO", "bb"));
    }
    }