package com.test;   import java.io.*;import java.util.*;import org.w3c.dom.*;import javax.xml.parsers.*;   public class MyXMLReader{   public static void main(String arge[]){   long lasting =System.currentTimeMillis();   try{     File f=new File("data_10k.xml");    DocumentBuilderFactory factory=DocumentBuilderFactory.newInstance();    DocumentBuilder builder=factory.newDocumentBuilder();    Document doc = builder.parse(f);    NodeList nl = doc.getElementsByTagName("VALUE");    for (int i=0;i<nl.getLength();i++){     System.out.print("车牌号码:" + doc.getElementsByTagName("NO").item(i).getFirstChild().getNodeValue());     System.out.println("车主地址:" + doc.getElementsByTagName("ADDR").item(i).getFirstChild().getNodeValue());   }   }catch(Exception e){    e.printStackTrace();   } 

解决方案 »

  1.   

    谢谢你的程序,我试了一下你的程序,已经改成我所需要的形式没有内容输出,你看一下我的程序,程序实现是将step AP203转成的xml文件读入,然后遍历所有的节点,本来我想取出其中的例如零件-轴的信息,结果没能实现。因为不知道该遍历上面所示的xml结构里面的哪个属性。麻烦帮我看一下,谢谢
    我得程序:
    import java.io.*;
    import javax.xml.parsers.DocumentBuilderFactory;
    import javax.xml.parsers.DocumentBuilder;
    import javax.xml.parsers.ParserConfigurationException;
    import org.w3c.dom.*;
    public class testxmldom
    {
        static void printxml (Element root,String stmp)
        {
          int n1,n2;
          System.out.print(stmp+"<"+root.getTagName());//分析属性 beging,用 root.getAttribute("name") 可以明文取值
         NamedNodeMap mlist=root.getAttributes();
         if (mlist!=null)
          {
             for (n1=0;n1<mlist.getLength();n1++)
              {
             System.out.print(" "+mlist.item(n1).getNodeName()+"=\""+mlist.item(n1).getNodeValue()+"\"");
              }
           }
    //分析属性 end
         System.out.println(">");
         NodeList nlist=root.getChildNodes();
         //NodeList ages = student.getElementsByTagName("年龄"); 
         if (nlist!=null)
          {
             for(n1=0;n1<nlist.getLength();n1++)
               {
                 if (nlist.item(n1).getNodeType()==3) //3是text类型
                   System.out.println(stmp+nlist.item(n1).getNodeValue().trim());
                   //1说明是一个子节点
                 if (nlist.item(n1).getNodeType()==1) printxml((Element)nlist.item(n1),stmp+"  ");           }
          }      System.out.println(stmp+"</"+root.getTagName()+">");
        }
        public static void main (String argv [])
        throws IOException, DOMException, ParserConfigurationException,org.xml.sax.SAXException
        {
            DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance ();
    DocumentBuilder db = dbf.newDocumentBuilder ();
            Node nodetmp;
            Document doc =db.parse(new File("c:/2.xml"));
            Element root= doc.getDocumentElement();
    /*
           Element cc=doc.createElement("add");
           cc.setAttribute("name","text");
           cc.appendChild(doc.createTextNode("this is test"));
            root.appendChild(cc);
    */
            printxml(root,"");
    System.exit (0);
        }}