给你写了一个example,其中的config.xml就是你写的xml
import java.util.List;
import org.jdom.Document;
import org.jdom.Element;
import org.jdom.input.SAXBuilder;
/**
 * @author hzqi
 * Created on 2004-4-19
 */
public class Test
{
    public static void main(String[] args) throws Exception
    {
        SAXBuilder saxBuilder = new SAXBuilder();
        Document doc = saxBuilder.build("c:/config.xml");
        Element root = doc.getRootElement();
        List db = root.getChildren();
        //here,you can use for statement.but i use index of 0 for example
        Element user = (Element) db.get(0);
        Element url = user.getChild("Url");
        System.out.println("url="+url.getText());
        //do other things
    }
}

解决方案 »

  1.   

    你这样做的话,还是首先需要知道db.get(0),然后还要明确这里面的标签是Url!我想知道能不能设计一种xml格式,让程序可以用类似HashMap中的get()方法那样根据key(这里就是标签Url)取得value值?谢谢!
      

  2.   

    /**
     *@Author huafeng
     *@Version 1.0
     *@Create-Time 20031109
     */
    import org.jdom.*;
    import org.jdom.input.*;
    import java.io.*;
    import java.util.List;public class xmlTool 
    {
    public static void main(String[] args)
    throws IOException
    {
    try
    {
    SAXBuilder builder = new SAXBuilder();
    Document doc = builder.build(new FileInputStream("wap.xml"));
    Element root = doc.getRootElement();
    List data = root.getChildren();
    Element data0 = (Element)data.get(0);
    String s = data0.getText();
    Element data1 = (Element)data.get(1);
    Element action_id = data1.getChild("action_id");
    Element service_id = data1.getChild("service_id");
    Element mid = data1.getChild("mid");
    Element mobile_id = data1.getChild("mobile_id");
    Element access_mode = data1.getChild("access_mode");



    List ac = data1.getChildren("access_mode");
    Element ac0 = (Element)ac.get(0);

    String sss = ac0.getText();
    System.out.println(sss);
    Element ac1 = (Element)ac.get(1);
    String ss1 = ac1.getText();
    System.out.println(ss1);
    Element ac2 = (Element)ac.get(2);
    String ss2 = ac2.getText();
    System.out.println(ss2);
    //=====================================================

    String scommand_name = data0.getText();
    String saction_id = action_id.getValue();
    String sservice_id = service_id.getText();
    String smid = mid.getText();
    String smobile_id = mobile_id.getValue();
    String saccess_mode = access_mode.getText();
    int isaction_id = Integer.parseInt(saction_id);
    int isservice_id = Integer.parseInt(sservice_id);

    System.out.println(scommand_name);
    System.out.println(isaction_id);
    System.out.println(isservice_id);
    System.out.println(smid);
    System.out.println(smobile_id);
    System.out.println(saccess_mode);


    }catch(IOException e)
    {}
    catch(JDOMException e)
    {}
    }
    }