大侠门帮帮忙:关于jdom怎么解析XML流,我之前用jdom解析的是XML文件,现在要解析xml流要怎么解析?(我试过把原来解析文件的参数Document read_doc=builder.build("Assist.xml")改成Document read_doc=builder.build(str) str是XML流字符串型式,解析会出错,请问要怎么解析?)

解决方案 »

  1.   

    SAXBuilder builder=new SAXBuilder();
           Document read_doc=builder.build("agAssistgetBulletin.xml");
           Element root=read_doc.getRootElement();
           Element cont = root.getChild("Content");
           List list=cont.getChildren("Bulletin");
           for(int i=0;i<list.size();i++){
               Element e=(Element)list.get(i);
               String bull_name=e.getChildText("Subject");
               String bull_dept=e.getChildText("PublishDept");
               String bull_date=e.getChildText("PublishDate");
               System.out.println("---------Bulletin--------------");
               System.out.println("NAME:"+bull_name);
               System.out.println("DEPT:"+bull_dept);
               System.out.println("DATE:"+bull_date);
               System.out.println("------------------------------");
               System.out.println();
    这是偶解析XML文件的函数,解析XML流,要怎么写?(帮帮忙,急)
      

  2.   

    import java.io.FileInputStream;
    import java.io.IOException;
    import java.io.FileNotFoundException;
    import org.jdom.Document;
    import org.jdom.Element;
    import org.jdom.JDOMException;
    import org.jdom.adapters.XercesDOMAdapter;
    import org.jdom.input.DOMBuilder;
    import org.jdom.output.XMLOutputter;
    import java.util.List;
    import java.util.Iterator;
     
    public class JDOMCreateExample1 {
     
      
        private static DOMBuilder builder = null;
     
        public static void main(String args[]) throws IOException,FileNotFoundException {
     
            XercesDOMAdapter xercAdapter = new XercesDOMAdapter();
            org.w3c.dom.Document w3Dom = xercAdapter.getDocument(new FileInputStream("test.xml"),false);
     
            builder = new DOMBuilder("org.jdom.adapters.XercesDOMAdapter");        Document doc = builder.build(w3Dom);
     
            List childs = doc.getRootElement().getChildren("elementtest");
             
            Iterator itr = childs.iterator();
               while (itr.hasNext()) {
                       Element child = (Element)itr.next();
                  System.out.println(child.getText());
            }
                
            }
     
    }