//先建立文档工厂
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();         DocumentBuilder db = dbf.newDocumentBuilder();
//把某个xml文件解析到内存dom对象
//你可以指定你自己的文件名
        Document doc = db.parse("c:/roomtree.xml");
//搜索某个名称的所有标签,这里的room根据你的需要替换即可
        NodeList nl = doc.getElementsByTagName("room");
//得到的是一个数组,获得其中的第一项
//这里还可以加是否为空的判断(当文档中不存在你想要搜索的标签时返回空)
        Node my_node = nl.item(0);
//获得节点的值
        String message = my_node.getFirstChild().getNodeValue(); 

解决方案 »

  1.   

    找找解释XML文档的工具 如:Jdom 做起来就好做多了。
      

  2.   

    IBM XML4J Apache Xerces 也不错,
      

  3.   

    遍历查找 <year>,<month>,<number>这些结点,找到后,它的parent就是了要找的结点
      

  4.   

    哪有这么麻烦,用xquery多舒服呀,简单还方便!
      

  5.   

    感觉如果把解析结果都放到一个collection集合中的确很费事,效率也不是太高如果真是要对XML中的数据进行比较复杂的操作建议还是把解析结果放到数据表中,然后对表进行操作
      

  6.   

    xpath可以实现,我用了xquery应该更方便,程序运行需要saxon包。
    t1.xq查询文件内容如下:
    ---------------------------------
    xquery version "1.0";
    declare variable $file as xs:string external; 
    declare variable $year as xs:string external; 
    declare variable $month as xs:string external; 
    declare variable $number as xs:string external; 
      for $b in doc($file)//newspaper/publication 
      let $n :=($b/@ID,$b/node1/text(),$b/node2/text(),$b/node3/text()) 
      where string($b/year) =$year and string($b/month)=$month and string($b/number)=$number 
      return $n
    ----------------------------------t1.xml文件内容如下:
    ----------------------------------
    <newspaper>
    <publication ID="1056">
    <node1>mynode1-1056</node1>
    <year>2005</year>
    <month>02</month>
    <number>03</number>
    <node2>mynode2-1056</node2>
    <node3>mynode3-1056</node3>
    </publication>
    <publication ID="488">
    <node1>mynode1-488</node1>
    <year>2008</year>
    <month>08</month>
    <number>08</number>
    <node2>mynode2-488</node2>
    <node3>mynode3-488</node3>
    </publication>
    <publication ID="12345">
    <node1>mynode1-12345</node1>
    <year>2004</year>
    <month>12</month>
    <number>11</number>
    <node2>mynode2-12345</node2>
    <node3>mynode3-12345</node3>
    </publication>
    </newspaper>
    --------------------------------------java代码如下:
    --------------------------------------
        public static void testXq1() throws IOException, XPathException {
            String basePath=
                "D:\\jakarta-tomcat-4.1.29\\webapps\\test\\files\\test04\\";
           String year="2005",month="02",number="03";//查询条件
            final Configuration config = new Configuration();
            final StaticQueryContext sqc = new StaticQueryContext(config);
            final XQueryExpression exp = sqc.compileQuery(new FileReader(basePath+"t1.xq"));
            final DynamicQueryContext dynamicContext = new DynamicQueryContext(config);
            dynamicContext.setParameter("file", "files/test04/t1.xml");
            dynamicContext.setParameter("year", year);
            dynamicContext.setParameter("month",month);
            dynamicContext.setParameter("number",number);
            List list=exp.evaluate(dynamicContext);        //顺序打印id,node1,node2,node3的值
            for(int i=0;i<list.size();i++){
              NodeInfo ni= (NodeInfo)list.get(i);
                System.out.println(ni.getStringValue());
            }
        }
    -----------------------------------打印结果:
    1056
    mynode1-1056
    mynode2-1056
    mynode3-1056如果需要xpath查询说一声!
      

  7.   

    xslt适合做这样的工作 需要学习下xpath
    xalan应该是最常用的吧?