你的xml文件必须有一个根节点,如下:
<products>
   <product>
       <id>111</id>
       <name>abc</name>
   </product>
   <product>
       <id>222</id>
       <name>eee</name>
   </product>
</products>
解析程序如下:
import javax.xml.parsers.*;
import org.w3c.dom.*;public class xmltest
{
public static void  main(String args[]){
    try{
       DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
       DocumentBuilder builder=factory.newDocumentBuilder();
       Document doc=builder.parse("test.xml");
       doc.normalize();
       NodeList links =doc.getElementsByTagName("product");
       for (int i=0;i<links.getLength();i++){
            Element link=(Element) links.item(i); 
            if (link.getElementsByTagName("id").item(0).getFirstChild().getNodeValue().toString().equals("222"))
                      System.out.println("name:"+link.getElementsByTagName("name").item(0).getFirstChild().getNodeValue());
       }
     }catch(Exception e){}
  }
}

解决方案 »

  1.   

    javax.xml.parsers.DocumentBuilderFactory dbf=javax.xml.parsers.DocumentBuilderFactory.newInstance();
          javax.xml.parsers.DocumentBuilder db=dbf.newDocumentBuilder();
          org.w3c.dom.Document document=db.parse(request.getRealPath("xxx.xml"));
          org.w3c.dom.Element element=document.getDocumentElement();
          org.w3c.dom.NodeList nl=element.getElementsByTagName("product");
        for(int i=0;i<nl.getLength();i++) {
          org.w3c.dom.Element ele = (org.w3c.dom.Element) nl.item(i);
          org.w3c.dom.NodeList nl1=ele.getElementsByTagName("id");
          if(nl1.getLength()==1) {
            org.w3c.dom.Element e=(org.w3c.dom.Element)nl1.item(0);
            org.w3c.dom.Text text=(org.w3c.dom.Text)e.getFirstChild();
    System.out.println(text);
          }
        }
    俺的文档里有,如下:
    http://www.csdn.net/develop/read_article.asp?id=17818
      

  2.   

    用XSLT:
    XML FILE :
    <products>
       <product>
           <id>111</id>
           <name>abc</name>
       </product>
       <product>
           <id>222</id>
           <name>eee</name>
       </product>
    </products>
    XSLT:
    <?xml version = "1.0" encoding = "UTF-8"?>
    <xsl:transform xmlns:xsl = "http://www.w3.org/1999/XSL/Transform" version = "1.0">
    <xsl:template match = "products">
    <xsl:element name = "NAME">
    <xsl:for-each select = "./product">
    <xsl:variable name = "id" select = "./id"/>
    <xsl:if test = "$id=222">
    <xsl:value-of select = "./name"/>
    </xsl:if>
    </xsl:for-each>
    </xsl:element>
    </xsl:template>
    </xsl:transform>
      

  3.   

    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;
        }    public static String getValue(Node elem)
        {
            String val = null;
            NodeList nlist = elem.getChildNodes();
            for(int i = 0; i < nlist.getLength(); i++)
            {
                Node child = nlist.item(i);
                if(child.getNodeType() == 3)
                {
                    String sval = child.getNodeValue();
                    if(sval.trim().length() > 0)
                        val = sval;
                    continue;
                }
                if(child.getNodeType() != 4)
                    continue;
                val = child.getNodeValue();
                break;
            }        if(val != null)
            {
                if(val.startsWith("\n"))
                    val = val.substring(1);
                if(val.endsWith("\n"))
                    val = val.substring(0, val.length() - 1);
            }
            return val;
        }
      

  4.   

    请看 
       public String getResponse(String inputReq)
        {
          //Parse who get the Information
          try
          {
           SAXBuilder builder = new SAXBuilder();
           Document InfoRequest = builder.build(new StringReader(inputReq));
         Element InfoRequestRoot = InfoRequest.getRootElement();
         String providerID = InfoRequestRoot.getAttribute("PROVIDERID").getValue();
         String clientID = InfoRequestRoot.getChild("CLIENTID").getText();
         String clientName = InfoRequestRoot.getChild("CLIENTNAME").getText();
         String fileID = InfoRequestRoot.getChild("FILEID").getText();
         String fileTitle = InfoRequestRoot.getChild("FILETITLE").getText();
        
         Element ResponseRoot = new Element("RESPONSE");
         Attribute response = new Attribute("PROVIDERID","1");
         ArrayList responseAttribute = new ArrayList();
         responseAttribute.add(response);
        
         ResponseRoot.setAttributes(responseAttribute);
         ResponseRoot.addContent(new Element("CLIENTID").setText(clientID));
         ResponseRoot.addContent(new Element("CLIENTNAME").setText(clientName));
         ResponseRoot.addContent(new Element("FILEID").setText(fileID));
         ResponseRoot.addContent(new Element("FILETITLE").setText(fileTitle));
         ResponseRoot.addContent(new Element("STATE").setText("READ"));    
         Document ResponseDoc = new Document(ResponseRoot);
         XMLOutputter outputter = new XMLOutputter(" ",true);
         return outputter.outputString(ResponseDoc);   
        
          }
          catch(Exception e)
          {
           e.printStackTrace(System.out);
           return "Error";
          }
        }