xml 格式如下:
<?xml version="1.0" encoding="UTF-8"?>
<ConfList>
  <conf confid="210834" username="admin" topic="远程测试2" begintime="2010-01-29 11:07:21" endtime="2010-02-01 11:06:00" content="备注" conftype="publicconf" confshow="1" audiotype="5" maxpersonspeak="9" attendnum="30" driftnum="0" videotype="320" bandwidth="256" maxvideo="24" encrypt="0"/>
  <conf confid="253378" username="admin" topic="远程测试1" begintime="2009-11-27 09:41:44" endtime="2009-12-27 09:41:00" content="备注" conftype="publicconf" confshow="1" audiotype="5" maxpersonspeak="9" attendnum="30" driftnum="0" videotype="320" bandwidth="256" maxvideo="24" encrypt="0"/>
</ConfList>要求读出xml中的各项数据值!

解决方案 »

  1.   

    根据各个节点的名字读取!!element元素以及元素中attribute的名字来获取其值(Jdom)
      

  2.   

    详见
    http://hi.baidu.com/pluto_andy/blog/item/49d4f1c35bda41120ff4776b.html
      

  3.   

    public static  void main(String[] args){
    String xmlFilePath = "Sp-Config.xml";
    try {
    String filePath = null; filePath = xmlFilePath;
    Document document = new SAXReader().read(
    Thread.currentThread().getContextClassLoader().getResourceAsStream(filePath)
    );

    List listWsdlAddr = document.selectNodes("//SpConfig/WsdlAddr");
    System.out.println(listWsdlAddr.size());
    for (Iterator iter = listWsdlAddr.iterator(); iter.hasNext();){
    Element element = (Element) iter.next();
    String s = element.getStringValue();
    System.out.println(s);
    }
    List listServiceName  =document.selectNodes("//SpConfig/ServiceConfig");
    for (Iterator iter = listServiceName.iterator(); iter.hasNext();){
    Element element = (Element) iter.next();
    String s = element.attributeValue("serviceName");
    System.out.println(s);
    }
    } catch (Exception e) {
    e.printStackTrace();
    }
    }
      

  4.   

    Element root = doc.getDocumentElement();
    NodeList elements = root.getChildNodes();
    for (int i = 0; i < elements.getLength(); i++) {
    Node element = elements.item(i);
    if (element.getNodeType() == Node.ELEMENT_NODE) {
    Element e = (Element) element;
    String confid = e.getAttribute("confid");
    beans.put(beanName, bean);
    }
    }

    return container;
    <conf confid="210834" username="admin" topic="远程测试2" begintime="2010-01-29 11:07:21" endtime="2010-02-01 11:06:00" content="备注" conftype="publicconf" confshow="1" audiotype="5" maxpersonspeak="9" attendnum="30" driftnum="0" videotype="320" bandwidth="256" maxvideo="24" encrypt="0"/>  是一个element  confid都是此element的attribute
      

  5.   

    在eclipse中打出element.  在后面出现的方法中找吧,肯定有你想要的,记不清楚了,是attributevalue()吧
      

  6.   

    用altova xml spy生成Java代码然后就很简单了
      

  7.   


    import java.io.StringReader;import javax.xml.parsers.DocumentBuilderFactory;import org.w3c.dom.Document;
    import org.w3c.dom.Element;
    import org.w3c.dom.Node;
    import org.w3c.dom.NodeList;
    import org.xml.sax.InputSource;public class Test {

    public void analyticalXML(String xml){
    Document doc;
    try {
    doc = DocumentBuilderFactory.newInstance().newDocumentBuilder()
    .parse(new InputSource(new StringReader(xml)));
        Element root = doc.getDocumentElement();

    if (0<root.getAttributes().getLength()){
        System.out.println("根节点属性信息..........");
                    for (int a = 0; a < root.getAttributes().getLength(); a++) {
                        System.out.println(root.getAttributes().item(a)
                                .getNodeName()
                                + ":" + root.getAttributes().item(a).getNodeName());                 }
    }
       NodeList books = root.getChildNodes();
                for (int i = 0; i < books.getLength(); i++) {
                    Node book = books.item(i);
                    // 判断是不是子节点                 if (book.getNodeType() == Node.ELEMENT_NODE) {
                        // 获得子节点的所有属性名和值                     if (0 < book.getAttributes().getLength()) {
                            System.out.println("第" + i + "个子节点属性信息..........");
                            for (int b = 0; b < book.getAttributes().getLength(); b++) {
                                System.out.println(book.getAttributes().item(b)
                                        .getNodeName()
                                        + ":"
                                        + book.getAttributes().item(b)
                                                .getNodeValue());
                            }
                        }
                        //获得子节点的子节点                     for (int j = 0; j < book.getChildNodes().getLength(); j++) {
                            Node book1 = book.getChildNodes().item(j);
                            //System.out.println(book.getChildNodes().item(j));                         // 判断是不是子节点                         if (book1.getNodeType()==Node.ELEMENT_NODE) {
                                // 获得子节点的所有属性名和值                             if (0 < book1.getAttributes().getLength()) {
                                    System.out.println(" 第" + i + "个子节点的第" + j
                                            + "子节点的属性信息..........");
                                    for (int b1 = 0; b1 < book1.getAttributes()
                                            .getLength(); b1++) {
                                        System.out.println(book1.getAttributes()
                                                .item(b1).getNodeName()
                                                + ":"
                                                + book1.getAttributes().item(b1)
                                                        .getNodeValue());
                                    }
                                }
                                System.out.println(book1.getNodeName()+":"+book1.getFirstChild().getNodeValue());
                            }                        
                        }
                    }                
                }
            } catch (Exception e) {
                e.printStackTrace();
            }     }

    public static void main(String [] args){
    Test t=new Test();
    String xml="<?xml version=\"1.0\" encoding=\"UTF-8\"?>" +
    "<ConfList>" +
    "<conf confid=\"210834\" username=\"admin\" topic=\"远程测试2\" begintime=\"2010-01-29 11:07:21\" endtime=\"2010-02-01 11:06:00\" content=\"备注\" conftype=\"publicconf\" confshow=\"1\" audiotype=\"5\" maxpersonspeak=\"9\" attendnum=\"30\" driftnum=\"0\"  bandwidth=\"256\" maxvideo=\"24\" encrypt=\"0\"/>" +
    "<conf confid=\"253378\" username=\"admin\" topic=\"远程测试1\" begintime=\"2009-11-27 09:41:44\" endtime=\"2009-12-27 09:41:00\" content=\"备注\" conftype=\"publicconf\" confshow=\"1\" audiotype=\"5\" maxpersonspeak=\"9\" attendnum=\"30\" driftnum=\"0\"  bandwidth=\"256\" maxvideo=\"24\" encrypt=\"0\"/>" +
    "</ConfList> ";
    t.analyticalXML(xml);
    }

    }
    运行结果:
    第0个子节点属性信息..........
    attendnum:30
    audiotype:5
    bandwidth:256
    begintime:2010-01-29 11:07:21
    confid:210834
    confshow:1
    conftype:publicconf
    content:备注
    driftnum:0
    encrypt:0
    endtime:2010-02-01 11:06:00
    maxpersonspeak:9
    maxvideo:24
    topic:远程测试2
    username:admin
    第1个子节点属性信息..........
    attendnum:30
    audiotype:5
    bandwidth:256
    begintime:2009-11-27 09:41:44
    confid:253378
    confshow:1
    conftype:publicconf
    content:备注
    driftnum:0
    encrypt:0
    endtime:2009-12-27 09:41:00
    maxpersonspeak:9
    maxvideo:24
    topic:远程测试1
    username:admin
    呵呵,楼主我也不会。刚写的。。试试吧。。
      

  8.   

    实际上这半个可以去掉。在子节点还有子节点的情况下可以如此判断     //获得子节点的子节点                            for (int j = 0; j < book.getChildNodes().getLength(); j++) {
                                    Node book1 = book.getChildNodes().item(j);
                                    //System.out.println(book.getChildNodes().item(j));                                // 判断是不是子节点                                if (book1.getNodeType()==Node.ELEMENT_NODE) {
                                        // 获得子节点的所有属性名和值                                    if (0 < book1.getAttributes().getLength()) {
                                            System.out.println(" 第" + i + "个子节点的第" + j
                                                    + "子节点的属性信息..........");
                                            for (int b1 = 0; b1 < book1.getAttributes()
                                                    .getLength(); b1++) {
                                                System.out.println(book1.getAttributes()
                                                        .item(b1).getNodeName()
                                                        + ":"
                                                        + book1.getAttributes().item(b1)
                                                                .getNodeValue());
                                            }
                                        }
                                        System.out.println(book1.getNodeName()+":"+book1.getFirstChild().getNodeValue());
                                    }                        
                                }
      

  9.   


    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 TestNet { public static void main(String[] args) throws ParserConfigurationException, SAXException, IOException {

    DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
    DocumentBuilder db = documentBuilderFactory.newDocumentBuilder();

    Document d = db.parse(new File("E:\\fate\\mynewproforstruts2\\javaxml\\src\\TestNet.xml"));

    Element root = d.getDocumentElement();//获取根元素对象

    System.out.println("根元素:"+root.getNodeName());

    NodeList dates = root.getElementsByTagName("conf");//获取元素名为conf的所有元素集合

    for(int i=0;i<dates.getLength();i++){//遍历上面取得的集合
    Element e = (Element)dates.item(i);//获取第i个元素
    System.out.println("子元素"+e.getNodeName()+"属性:");
    parseAttributes(e);

    }
    }


    public static  void parseAttributes(Element e){//解析元素属性并打印出来
    NamedNodeMap nnm = e.getAttributes();//获取属性集合
    for(int i=0;i<nnm.getLength();i++){
    Node n = nnm.item(i);//遍历属性
    System.out.print("\t");
    System.out.println(n.getNodeName()+" : "+n.getNodeValue());
    }
    }}
      

  10.   

    用dom4j去解析,很快的,dom4j也很容易上手
      

  11.   

    我只想给一点提醒
    呵呵
    别忘记导入dom4j架包