这个恐怕不是xml的强项,如果一定要实现要比通过数据库要麻烦,
你可以动态生成xsl,通过xsl的条件判断选择需要输出的节点值。

解决方案 »

  1.   

    public static Document parseXML(InputStream input)
            throws UnsupportedEncodingException, SAXException, IOException, ParserConfigurationException
        {
            DocumentBuilder builder = null;
            synchronized(com.justnorth.util.Tool.class)
            {
                if(docFactory == null)
                    docFactory = DocumentBuilderFactory.newInstance();
            }
            builder = docFactory.newDocumentBuilder();
            BufferedReader br = new BufferedReader(new InputStreamReader(input, "UTF8"));
            InputSource is = new InputSource(br);
            return builder.parse(is);
        }
        public static Document parseXML(String input)
            throws UnsupportedEncodingException, SAXException, IOException, ParserConfigurationException
        {
            DocumentBuilder builder = null;
            synchronized(com.justnorth.util.Tool.class)
            {
                if(docFactory == null)
                    docFactory = DocumentBuilderFactory.newInstance();
            }
            builder = docFactory.newDocumentBuilder();
            //BufferedReader br = new BufferedReader(new InputStreamReader(input, "gb2312"));
            InputSource is = new InputSource(new StringReader(input));
            return builder.parse(is);
        }
        public static String getAttribute(Element elem, String name)
        {
            Attr attr = elem.getAttributeNode(name);
            return attr != null ? attr.getValue() : null;
        }
      

  2.   

    谢谢楼上的两位 To;signboy(横) 
     谢谢你的代码,不过你好象误解我意思了,可能是我没说明白。。
     我的意思是通过已知的多个条件,查找出XML 中符合所有条件的结点。。 如果单纯用XQUERY , XQL 应该能做到,不过,不太熟悉XQUERY 和XQL 是否有java的接口?? 要在其中加上验证和返值的过程。。已经有开发好的SCHEMA 是我的毕业设计中的一部分,不知道能否实现?? 
      

  3.   

    select.xml:
    <?xml version="1.0"?>
    <?xml-stylesheet type="text/xsl" href="select.xsl"?>
    <select>
     <service>
      <type>sendmail</type>
      <mailto>[email protected]</mailto>
      <from>[email protected]</from>
     </service>
     <service>
      <type>upload</type>
      <filename>xml.pdf</filename>
      <size>1024K</size>
     </service>
    </select>
     
    select.xsl:
    <?xml version="1.0"?>
    <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
             version="1.0"
    >
    <xsl:template match="/">
    <html>
     <head>
      <title>select result</title>
     </head>
     <body>
      <xsl:for-each select="select/service">
       <xsl:choose select="type">
        <xsl:when test="type='sendmail'">
         <b><xsl:value-of select="mailto"/></b><br/>
         <b><xsl:value-of select="from"/></b><br/>
        </xsl:when>
        <xsl:otherwise>
         <i><xsl:value-of select="filename"/></i><br/>
         <i><xsl:value-of select="size"/></i><br/>
        </xsl:otherwise>
       </xsl:choose> 
      </xsl:for-each>
     </body>
    </html>
    </xsl:template>
    </xsl:stylesheet>xsl文件你可以通过程序动态的生成,来满足你的查询条件
      

  4.   

    还可以用<xsl:if test="statement"></xsl:if>来进行判断输出