呵呵,后一个<publication>标记还写错了呢,应该</publication>

解决方案 »

  1.   

    import org.w3c.dom.*;
    import javax.xml.parsers.*;
    import java.io.*;public class Hello{
        public static void main(String[] args) throws Exception{
            new Hello();
        }    public Hello() throws Exception{
            DocumentBuilderFactory Factory = DocumentBuilderFactory.newInstance();
            DocumentBuilder DBuilder = Factory.newDocumentBuilder();
            StringBuffer xmlString = new StringBuffer();
            xmlString.append("<newspaper>");
            
            xmlString.append("<publication ID=\"1056\">");
            xmlString.append("<year>2000</year>");
            xmlString.append("<month>08</month>");
            xmlString.append("<number>26</number>");
            xmlString.append("</publication>");
            xmlString.append("<publication ID=\"1057\">");
            xmlString.append("<year>2105</year>");
            xmlString.append("<month>12</month>");
            xmlString.append("<number>03</number>");
            xmlString.append("</publication>");
            xmlString.append("<publication ID=\"1056\">");
            xmlString.append("<year>2005</year>");
            xmlString.append("<month>12</month>");
            xmlString.append("<number>25</number>");
            xmlString.append("</publication>");
            xmlString.append("<publication ID=\"1058\">");
            xmlString.append("<year>2035</year>");
            xmlString.append("<month>06</month>");
            xmlString.append("<number>13</number>");
            xmlString.append("</publication>");        xmlString.append("</newspaper>");
            
            StringReader iostring = new StringReader(xmlString.toString());
            org.xml.sax.InputSource is = new org.xml.sax.InputSource(iostring);
            Document XML = DBuilder.parse(is);
            XML.normalize();
            Node xml = XML.getFirstChild();        System.out.println("get root tag is:" + xml.getNodeName());
            NodeList list = xml.getChildNodes();
            System.out.println("root tag size is:" + list.getLength());
            for(int i = 0; i < list.getLength(); i++){
                Node curItem = list.item(i);
                if(curItem == null || 
                   !curItem.hasChildNodes() ||
                   !curItem.hasAttributes() ||
                   !curItem.getNodeName().equalsIgnoreCase("publication") ||
                   !curItem.getAttributes().getNamedItem("ID").getNodeValue().
                   equals("1056")){
                    System.out.println("Wrong Node,Skip it.");
                    continue;
                }
                //String curYear = curItem.getChildNodes();
                System.out.println("Bingo!Got One!");
                NodeList curBingoList = curItem.getChildNodes();
                for(int j = 0; j < curBingoList.getLength(); j++){
                    System.out.println(curBingoList.item(j).getNodeName() + "=" + 
                                       curBingoList.item(j).getFirstChild().getNodeValue());
                }
            }
        }
    }
      

  2.   

    我想可能是我提问时候没有表达清楚,我要实现的是:在我建立好(已经写入)的xml文档中,通过提供作为参数的某些结点值,例如<year>,<month>的值,查询得到相应其他结点的值,如<publication>,<number>等。
      

  3.   

    xml解析常用的节点访问,属性访问方法都在这里面了,自己根据具体需要去调用不就行了。
      

  4.   

    /*
     * Created on 2004-8-4 by Jem.Lee <李克喜>
     *
     * XmlPaser.java is a part of cn.bs.common.control 
     * ALL RIGHTS RESERVED.
     * COPYRIGHT (C) 2004. Jem.Lee <李克喜>
     *//* 包路径 cn.bs.common.control*/
    package wst;import java.io.FileInputStream;
    import java.io.FileNotFoundException;
    import java.io.IOException;
    import java.net.MalformedURLException;import org.jdom.Attribute;
    import org.jdom.Document;
    import org.jdom.Element;
    import org.jdom.JDOMException;
    import org.jdom.input.SAXBuilder;/*Import包内容*/
    /**
     * <PRE>
     * History
     * 2004-8-4 1:29:41 created by Jem.Lee <李克喜>
     * 
     * 目的
     * 解释配置文件,并从配置文件中取得相应的信息
     * </PRE>
     */
    public class Control_xml2  {

    String filepath = "";
    SAXBuilder sb = null;
    Document doc = null;
    FileInputStream file = null;

    /**
     * XML文件处理的构造器
     * @param path
     * @throws FileNotFoundException
     * @throws JDOMException
     * @throws MalformedURLException
     */
    public Control_xml2(String path) throws FileNotFoundException, JDOMException, MalformedURLException{
    filepath = path;
    try {
    sb = new SAXBuilder();
    file =  new FileInputStream(filepath);
    try {
    doc =  sb.build(file);//注意改成一个配置属性文件
    } catch (IOException e1) {
    // TODO 自动生成 catch 块
    e1.printStackTrace();
    }
    } catch (FileNotFoundException e) {
    throw e;
    } catch (JDOMException e) {
    throw e;
    }
    }
    /**
     * <PRE> 
     * 释放对象
     * @throws IOException
     * </PRE>
     */
    public void CloseFile() throws IOException{
    file.close();
    file = null;
    doc  = null;
    sb = null;
    }
        
        /**
         * <PRE> 
         * 通过事件ID获得处理该事件的Servlet 
         * @param eventid 事件ID
         * @return 处理Servlet 
         * </PRE>
         */
        public String get_year(String eventid) {
            
           
            String year="";
           
            /*查询跳出标记*/
            boolean breakflag = false;
            
            try {
                
                //从文件构造一个Document,并装载了配置XML文件。
                
                Element root = doc.getRootElement(); //得到根元素
                java.util.List newspaper = root.getChildren(); //获得页面的集合
                /*页面循环*/
                for (int i = 0; i < newspaper.size(); i++) {
                    Element publication = (Element)newspaper.get(i); //获得一个页面
                    Attribute ID = config.getAttribute("ID"); //获得ID属性
                    Attribute year = config.getAttribute("year");//获得year属性
                    
                            
                            /*获取对应的_yearServlet*/
                            if(eventid.equals(ID.getValue())){
                                year = year.getValue();
                               
                                breakflag = true;
                                break;
                            }
                        
                        /*如果已经找到则跳出*/
                        if(breakflag){
                            break;
                        }
                    }
                    /*如果已经找到则跳出*/
                  
                
            } catch (Exception ex) {
                System.out.print(ex.getMessage());
                
            }        return year;
        }
        
        public String get_month(String eventid) {
            
           
            String month="";
           
            /*查询跳出标记*/
            boolean breakflag = false;
            
            try {
                
                //从文件构造一个Document,并装载了配置XML文件。
                
                Element root = doc.getRootElement(); //得到根元素
                java.util.List newspaper = root.getChildren(); //获得页面的集合
                /*页面循环*/
                for (int i = 0; i < newspaper.size(); i++) {
                    Element publication = (Element)newspaper.get(i); //获得一个页面
                    Attribute ID = config.getAttribute("ID"); //获得ID属性
                    Attribute month = config.getAttribute("month");//获得month属性
                    
                            
                            /*获取对应的_monthServlet*/
                            if(eventid.equals(ID.getValue())){
                                month = month.getValue();
                               
                                breakflag = true;
                                break;
                            }
                        
                        /*如果已经找到则跳出*/
                        if(breakflag){
                            break;
                        }
                    }
                    /*如果已经找到则跳出*/
                  
                
            } catch (Exception ex) {
                System.out.print(ex.getMessage());
                
            }        return month;
        }
        
       public String get_number(String eventid) {
            
           
            String number="";
           
            /*查询跳出标记*/
            boolean breakflag = false;
            
            try {
                
                //从文件构造一个Document,并装载了配置XML文件。
                
                Element root = doc.getRootElement(); //得到根元素
                java.util.List newspaper = root.getChildren(); //获得页面的集合
                /*页面循环*/
                for (int i = 0; i < newspaper.size(); i++) {
                    Element publication = (Element)newspaper.get(i); //获得一个页面
                    Attribute ID = config.getAttribute("ID"); //获得ID属性
                    Attribute number = config.getAttribute("number");//获得number属性
                    
                            
                            /*获取对应的_numberServlet*/
                            if(eventid.equals(ID.getValue())){
                                number = number.getValue();
                               
                                breakflag = true;
                                break;
                            }
                        
                        /*如果已经找到则跳出*/
                        if(breakflag){
                            break;
                        }
                    }
                    /*如果已经找到则跳出*/
                  
                
            } catch (Exception ex) {
                System.out.print(ex.getMessage());
                
            }        return number;
        }
       
    }
      

  5.   

    使用的时候
    String path=getServletContext().getRealPath("***.xml");//你的xml一般放在web-inf/下


    Control_xml2 xml=new Control_xml2(path);
                       xml.get_month(id);
    ....
    ..
    ..