<?xml version="1.0" encoding="UTF-8"?>
<beans>
<bean id="computer" class="com.yangtianb.entity.Computer">
</bean> 
</beans>我要得到“computer” 和“com.yangtianb.entity.Computer”也就是id,class的值

解决方案 »

  1.   

    看看 Spring 的源代码就知道了。
      

  2.   

    也不一定要看Srping源码
    去看看XML的解析方式:Jdom,dom4j之类的就行
      

  3.   


    import java.io.File;import org.dom4j.*;
    import org.dom4j.io.SAXReader;
    public class dom4jComputers { public static void main(String[] args) throws DocumentException {
    SAXReader saxReader = new SAXReader();

    Document doc = saxReader.read(new File("c:/computers.xml"));
    doc.accept(new Visitor());
    } private static class Visitor extends VisitorSupport{
    public void visit(Attribute node){
    System.out.println(node.getName() + "  " + node.getText());
    }
    }
    }
    /* Output:
    id  computer
    class  com.yangtianb.entity.Computer
    *///:-
      

  4.   


    package com.luowei.parseXML.dom;
    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.NodeList;
    import org.xml.sax.SAXException;public class ParseXML {
    public static void main(String[] args) {
    try {
    DocumentBuilderFactory factory = DocumentBuilderFactory
    .newInstance();
    DocumentBuilder builder = factory.newDocumentBuilder();
    // 获取XML文件
    Document document = builder.parse("你XML文件的名称");
    // 根据元素的名称获取节点
    NodeList nodeList = document.getElementsByTagName("beans"); for (int i = 0; i < nodeList.getLength(); i++) {
    // 获取子节点
    NodeList childNodeList = nodeList.item(i).getChildNodes(); for (int j = 0; j < childNodeList.getLength(); j++) {
    String uuid = null;
    String className = null;
                                            //判断此元素是否有属性
    if (childNodeList.item(j).hasAttributes()) {
    // 获取元素的属性值
    Element element = (Element) childNodeList.item(j);
    uuid = element.getAttribute("id");
    className = element.getAttribute("class");
    System.out.print("id:  "+uuid+" class: "+className);
    }
    }
    }
    } catch (ParserConfigurationException e) {
    e.printStackTrace();
    } catch (SAXException e) {
    e.printStackTrace();
    } catch (IOException e) {
    e.printStackTrace();
    } }
    }
      

  5.   

    这样也可以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.NamedNodeMap;
    import org.w3c.dom.Node;
    import org.w3c.dom.NodeList;
    import org.xml.sax.SAXException;public class FileReader
    {
    public static void main(String[] args)
    {
    try
    {
    readXML();
    } catch (ParserConfigurationException e)
    {
    // TODO Auto-generated catch block
    e.printStackTrace();
    } catch (SAXException e)
    {
    // TODO Auto-generated catch block
    e.printStackTrace();
    } catch (IOException e)
    {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }
    } private static void readXML() throws ParserConfigurationException,
    SAXException, IOException
    {
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    DocumentBuilder builder = factory.newDocumentBuilder();
    // 获取XML文件
    Document document = builder.parse("config.xml");
    // 根据元素的名称获取节点
    NodeList nodeList = document.getElementsByTagName("bean"); for (int i = 0; i < nodeList.getLength(); i++)
    {
    Node node = nodeList.item(i);
    NamedNodeMap map = node.getAttributes();
    for (int j = 0; j < map.getLength(); j++)
    {
    Node attr = map.item(j); if (attr.getNodeName().equals("id"))
    {
    System.out.println("id = " + attr.getNodeValue());
    }
    if (attr.getNodeName().equals("class"))
    {
    System.out.println("class = " + attr.getNodeValue());
    }
    }
    } }
    }