<?xml version="1.0" encoding="UTF-8"?>
<settingInfo xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<sessionData>
  <company id="1">
    <fxSchemas>
      <session name="XXXXXXXX" schema="XXXX"/>
      <session name="YYYYYYYY" schema="YYYY"/>
    </fxSchemas>
  </company>
  <company id="2">
    <fxSchemas>
      <session name="XXXXXXXX" schema="XXXX"/>
      <session name="YYYYYYYY" schema="YYYY"/>
    </fxSchemas>
  </company>
</sessionData>
</settingInfo>我要取得根据company id的值取得 <session name="XXXXXXXX" schema="XXXX">中name和schema中的值,<fxSchemas>中有多少个<session></session>就要取出多少个,请大虾帮帮忙 !!

解决方案 »

  1.   

    给个例子,自己整合,学到知识更牢固.package   xmlreader;   
      import   java.util.*;   
      import   java.io.*;   
        
      import   javax.xml.parsers.*;   
      import   javax.xml.transform.*;   
      import   javax.xml.transform.dom.DOMSource;   
      import   javax.xml.transform.stream.StreamResult;   
        
      import   org.w3c.dom.*;   
        
      import   org.xml.sax.*;   
        
      public   class   XmlPropertyReader   {   
              private   static   Map   propertyMap   =   null;   
              private   static   final   String   fileName   =   "/dbProperties.xml";   
        
        
              private   XmlPropertyReader()   {};   
        
              public   static   String   getProperty(String   key)   throws   Exception   {   
                      if   (propertyMap   ==   null)   {   
                              propertyMap   =   loadProperties();   
                      }   
        
                      if   (propertyMap.containsKey(key))   {   
                              return   (String)   propertyMap.get(key);   
                      }   else   {   
                              throw   new   Exception("Property   "   +   key   +   "   does   not   exist");   
                      }   
              }   
        
              private   static   Map   loadProperties()   throws   Exception   {   
                      Document   doc   =   null;   
        
                      try   {   
                              DocumentBuilder   builder   =   DocumentBuilderFactory.newInstance()   
                                                                                  .newDocumentBuilder();   
        
                              InputStream   is   =   XmlPropertyReader.class.getResourceAsStream(   
                                              fileName);   
                              doc   =   builder.parse(is);   
                              //ServletUtility.printXML(doc);   
        
                              return   getMap(doc.getElementsByTagName("property"));   
        
                      }   catch   (Exception   ex)   {   
                              throw   ex;   
                              //logger.error(ex);   
                      }   
              }   
        
              private   static   Map   getMap(NodeList   list)   throws   Exception   {   
                      Map   map   =   new   HashMap();   
        
                      if   (list   ==   null)   {   
                              return   map;   
                      }   
        
                      for   (int   i   =   0;   i   <   list.getLength();   i++)   {   
                              String   key   =   null;   
                              String   value   =   null;   
                              Node   parentNode   =   list.item(i);   
        
                              NamedNodeMap   attributes   =   parentNode.getAttributes();   
        
                              for   (int   j   =   0;   j   <   attributes.getLength();   j++)   {   
                                      Node   att   =   attributes.item(j);   
        
                                      if   (att.getNodeName().equals("key"))   {   
                                              key   =   att.getNodeValue();   
                                      }   else   if   (att.getNodeName().equals("value"))   {   
                                              value   =   att.getNodeValue();   
                                      }   else   {   
                                              throw   new   Exception(   
                                                              "Attribute   must   be   either   key   or   value");   
                                      }   
                              }   
        
                              if   ((key   ==   null)   ||   (value   ==   null))   {   
                                      throw   new   Exception("Either   key   or   value   is   not   present");   
                              }   
        
                              map.put(key,   value);   
                      }   
        
                      return   map;   
              }   
      }   本篇文章来自<A href='http://www.soidc.net'>IDC专家网</a> 原文链接:http://www.soidc.net/discuss/1/060620/23/140706_1.html
      

  2.   

    ...建议用 dom4j
    你也可以用jdom方式
      

  3.   

    我用的是dom4j ,
    getElementList(Element element){
    int id=element.attributeValue("id");
    int i=0;
          if (id == 0) {
                  for (Iterator it = element.elementIterator(); it.hasNext();) {
                  Element elm = (Element) it.next();
                  String name=elm.attributeValue("name");
                  String schema=elm.attributeValue("schema");
                  i++;
                  }
          } else {
             
              for (Iterator it = elements.iterator(); it.hasNext();) {
                  Element elem = (Element) it.next();
                  getElementList(elem);
              }
          }
      }
      

  4.   

    刚刚写了一个,你看看:
    public class Test {
    public Test() throws Exception{
    ClassLoader loader = Thread.currentThread().getContextClassLoader();
    URL fileurl=null;
    if(loader instanceof URLClassLoader){
    URLClassLoader ucl=(URLClassLoader)loader;
    fileurl=ucl.findResource("test.xml");
    }
    if(fileurl==null){
    fileurl=loader.getResource("test.xml");
    }
    if(fileurl==null){
    String message="There is no 'test.xml' file!";
    throw new IOException(message);
    }
    String file=URLDecoder.decode(fileurl.getFile(),"utf-8");
    Document doc=null;
    try{
    SAXReader reader=new SAXReader();
    doc=reader.read(new File(file));
    }catch(Exception e){
    System.out.println(e.getLocalizedMessage());
    }
    Element root = doc.getRootElement();
    for(Iterator x=root.element("sessionData").elementIterator("company");x.hasNext();){
    Element x_element=(Element)x.next();
    String id=x_element.attributeValue("id");
    for(Iterator y=x_element.element("fxSchemas").elementIterator("session");y.hasNext();){
    Element y_element=(Element)y.next();
    String name=y_element.attributeValue("name");
    String schema=y_element.attributeValue("schema");
    System.out.println("id="+id+" "+"name: "+name+" "+"schema: "+schema);
    }
    }
    }
    public static void main(String[] args) throws Exception {
    Test test=new Test();
    }
    }
      

  5.   

    我把你的XML改了,以边测试
    <?xml version="1.0" encoding="UTF-8"?> 
    <settingInfo xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> 
    <sessionData> 
      <company id="1"> 
        <fxSchemas> 
          <session name="XXXXXXXX" schema="1X1X"/> 
          <session name="YYYYYYYY" schema="1Y1Y"/> 
        </fxSchemas> 
      </company> 
      <company id="2"> 
        <fxSchemas> 
          <session name="XXXXXXXX" schema="2X2X"/> 
          <session name="YYYYYYYY" schema="2Y2Y"/> 
        </fxSchemas> 
      </company> 
    </sessionData> 
    </settingInfo> 
      

  6.   

    关于dom4j和jdom都可以用来读取XML文件
    分别用SAXReader和SAXBuilder来读取
    上面我用的是dom4j的SAXReader类
    如果用SAXBuilder的话可以这样写:
    String taskfile = URLDecoder.decode(profileurl.getFile(),"UTF-8");
    Document document;
    SAXBuilder builder=new SAXBuilder();
    document = (Document) builder.build(new File(taskfile));
    Element root=document.getRootElement();
    List list = root.getChildren();
    Element child=(Element)list.get(1);
    System.out.println(child.getAttributeValue("a"));
    LZ自己可以写写试试看!