原文件:
<?xml version="1.0" encoding="gb2312"?>
<students>
 <students id="1">
   <name>zhang</name>
   <sex>female</sex>
   <contact phone="138000001">
   </contact>
 </students>
  <students id="2">
   <name>li</name>
   <sex>male</sex>
   <contact phone="138000002">
     
   </contact> 
 </students>
</students>
程序:
import org.w3c.dom.*;import javax.xml.parsers.*;import java.io.*;import org.apache.xpath.XPathAPI;
public class test { /**
 * @param args
 */
public static void main(String[] args) {
// TODO Auto-generated method stub
    try{
     //InputStream
   String path="test.xml";
   DocumentBuilderFactory domFactory=DocumentBuilderFactory.newInstance();
   domFactory.setValidating(false);
   domFactory.setNamespaceAware(false);
   DocumentBuilder domBuilder=domFactory.newDocumentBuilder();
   Document document=domBuilder.parse(new File(path));        //开始分析文件

   String[] cond=new String[50];
   int i=0;
   
   
   cond[i]="students/students";                                         //寻找到所有JobParts节点
   NodeList JobParts=XPathAPI.selectNodeList(document,cond[i]);
   
   System.out.println(JobParts.getLength());
   i++;
   
   
for(int t=0;t<JobParts.getLength();t++){           //遍历每个JobPart节点

                        Node JobPart=JobParts.item(t);


String JobPartID=JobPart.getAttributes().getNamedItem("id").getNodeValue();
System.out.println("students"+JobPartID+":");       //寻找到每个JobPart节点的id

cond[i]="students/contact/@phone";
 Node JobContact=XPathAPI.selectSingleNode(JobPart,cond[i]);   //寻找到每个JobPart节点的ContactPhone
 String JobContactName,JobContactValue;
 JobContactName=JobContact.getNodeName();
 JobContactValue=JobContact.getNodeValue();
   System.out.print(JobContactName+":");
   System.out.println(JobContactValue);


}
   
  
   

         }
    catch (Exception e)
     {e.printStackTrace();}     

        }}结果是:
java.lang.NullPointerException
at test.main(test.java:46)
2
students1:
这是怎么回事呢

解决方案 »

  1.   

    你这结果有点问题, 如果报了空指针异常,就不太可能输出2 students1:了。代码太乱,都不知道哪是第46行
      

  2.   

    不好意思,原来可能写得太乱了,请您给看看这个吧
    原XML文件
    <?xml version="1.0" encoding="gb2312"?>
    <students>
     <JobPart id="1">
       <name>zhang</name>
       <sex>female</sex>
       <contact phone="138000001">
       </contact>
     </JobPart>
      <JobPart id="2">
       <name>li</name>
       <sex>male</sex>
       <contact phone="138000002">   
       </contact> 
     </JobPart>
    </students>
    程序:
    import org.w3c.dom.*;
    import javax.xml.parsers.*;
    import java.io.*;
    import org.apache.xpath.XPathAPI;
    public class test {
    public static void main(String[] args) {

     try{
        
    String path="test.xml";
    DocumentBuilderFactory domFactory=DocumentBuilderFactory.newInstance();
    domFactory.setValidating(false);
    domFactory.setNamespaceAware(false);
    DocumentBuilder domBuilder=domFactory.newDocumentBuilder();
    Document document=domBuilder.parse(new File(path));                   //开始分析文件

    String[] cond=new String[50];
    int i=0;
    cond[i]="students/JobPart";                                         //寻找到所有JobPart节点
    NodeList JobParts=XPathAPI.selectNodeList(document,cond[i]);
    System.out.println(JobParts.getLength());
    i++;
       
    for(int t=0;t<JobParts.getLength();t++){                            //遍历每个JobPart节点
    Node JobPart=JobParts.item(t);
    String JobPartID=JobPart.getAttributes().getNamedItem("id").getNodeValue();
    System.out.println("JobPart"+JobPartID+":");                   //寻找到每个JobPart节点的id

    cond[i]="JobPart/contact/@phone";
    Node JobContact=XPathAPI.selectSingleNode(JobPart,cond[i]);   //寻找到每个JobPart节点的ContactPhone

    String JobContactName=JobContact.getNodeName();  //第32行
    String JobContactValue=JobContact.getNodeValue();
    System.out.print(JobContactName+":");
    System.out.println(JobContactValue);
       }
    }
    catch (Exception e)
    {e.printStackTrace();}
      }
    }
    程序运行结果为:
    2
    JobPart1:
    java.lang.NullPointerException
    at test.main(test.java:32)其实我只是想在这个XML文件的 两个<JobPart>....</JobPart>节点中用XPath去寻找所需信息,我明白在整个XML文件中用XPath去寻找所需信息的方法,但是用相同的方法在里面的某个节点中去寻找信息怎么就不行了呢,谢谢高手帮忙啊!
      

  3.   


    import org.w3c.dom.*; import javax.xml.parsers.*; import java.io.*; import org.apache.xpath.XPathAPI; 
    public class test { /** 
    * @param args 
    */ 
    public static void main(String[] args) { 
    // TODO Auto-generated method stub 
        try{ 
        //InputStream 
      String path="test.xml"; 
      DocumentBuilderFactory domFactory=DocumentBuilderFactory.newInstance(); 
      domFactory.setValidating(false); 
      domFactory.setNamespaceAware(false); 
      DocumentBuilder domBuilder=domFactory.newDocumentBuilder(); 
      Document document=domBuilder.parse(new File(path));        //开始分析文件   String[] cond=new String[50]; 
      int i=0; 
      
      
      cond[i]="students/students";                                        //寻找到所有JobParts节点 
      NodeList JobParts=XPathAPI.selectNodeList(document,cond[i]); 
      
      System.out.println(JobParts.getLength()); 
      i++; 
      
      
    for(int t=0;t <JobParts.getLength();t++){          //遍历每个JobPart节点                         Node JobPart=JobParts.item(t); 
    String JobPartID=JobPart.getAttributes().getNamedItem("id").getNodeValue(); 
    System.out.println("students"+JobPartID+":");      //寻找到每个JobPart节点的id cond[i]="students/contact/@phone"; 
    Node JobContact=XPathAPI.selectSingleNode(JobPart,cond[i]);  //寻找到每个JobPart节点的ContactPhone 
    String JobContactName,JobContactValue; 
    JobContactName=JobContact.getNodeName(); 
    JobContactValue=JobContact.getNodeValue(); 
      System.out.print(JobContactName+":"); 
      System.out.println(JobContactValue); 

      
      
              } 
        catch (Exception e) 
        {e.printStackTrace();}             } } 
      

  4.   

    用的dom4j可以不?
    import java.util.List;
    import java.util.regex.Matcher;
    import java.util.regex.Pattern;import javax.xml.parsers.DocumentBuilderFactory;import org.dom4j.Document;
    import org.dom4j.Element;
    import org.dom4j.io.SAXReader;public class Test { public static void main(String[] args) { 
    // TODO Auto-generated method stub 
        try{ 
        //InputStream 
    //   String path="D:\\test.xml"; 
      DocumentBuilderFactory factory=DocumentBuilderFactory.newInstance();
    Element root=null,child=null;
    List jobparts=null;
    SAXReader saxReader = new SAXReader(); 
    Document xmldoc=saxReader.read("D:\\test.xml");
    root=xmldoc.getRootElement();      //开始分析文件  jobparts = root.elements("JobPart");

    for(int i=0;i<jobparts.size();i++){
    child = (Element)jobparts.get(i);
    System.out.print(":"+child.elementText("name")); 
      System.out.println(child.elementText("sex")); 
    }
            } 
        catch (Exception e) 
        {e.printStackTrace();}               }  } 
      

  5.   


    cond[i]="students/contact"; 
    Node JobContact=XPathAPI.selectSingleNode(JobPart,cond[i]);  //寻找到每个JobPart节点的ContactPhone String JobContactName=JobContact.getNodeName();  //第32行 
    String JobContactValue=JobContact.getAtrrebute();//类似的方法 
    System.out.print(JobContactName+":"); 
    System.out.println(JobContactValue);
    cond[i]="students/contact/@phone"; 
    好像不是节点啊,所以不存在这个节点当然JobContact这个就是null,调用空值对象所以包nullpointexeption
      

  6.   

    cond[i]="students/contact"; 改为这个
    JobPart/contact 上个
      

  7.   

    7楼,谢谢您的回复
    cond[i]="students/contact/@phone";
    这是查询条件,查询的是
    <contact phone="138000001"> 
    </contact> 
    其中的属性节点phone,如果换成在整个XML文件中查询,也就是
    Node JobContact=XPathAPI.selectSingleNode(document,cond[i]);  
    就可以查询出来,但是在JobPart中就查询不到
      

  8.   

    或许xml写的有问题吧
    <?xml version="1.0" encoding="gb2312"?>
    <students>
    <student id="1">
      <name>zhang </name>
      <sex>female </sex>
      <contact phone="138000001">
      </contact>
    </student>
      <student id="2">
      <name>li </name>
      <sex>male </sex>
      <contact phone="138000002">
       
      </contact>
    </student>
    </students> 
      

  9.   

    7楼,谢谢您的回复
    cond[i]="students/contact/@phone"; 这是查询条件,
    查询的是 
    <contact phone="138000001"> 
    </contact> 其中的属性节点phone,
    如果换成在整个XML文件中查询,也就是 Node JobContact=XPathAPI.selectSingleNode(document,cond[i]);  就可以查询出来,但是在JobPart中就查询不到
      

  10.   

    回复10楼,请您看这个
    原XML文件 
    <?xml version="1.0" encoding="gb2312"?> 
    <students> 
    <JobPart id="1"> 
      <name>zhang </name> 
      <sex>female </sex> 
      <contact phone="138000001"> 
      </contact> 
    </JobPart> 
      <JobPart id="2"> 
      <name>li </name> 
      <sex>male </sex> 
      <contact phone="138000002">  
      </contact> 
    </JobPart> 
    </students> 
    程序: 
    import org.w3c.dom.*; 
    import javax.xml.parsers.*; 
    import java.io.*; 
    import org.apache.xpath.XPathAPI; 
    public class test { 
    public static void main(String[] args) { try{ 
        
    String path="test.xml"; 
    DocumentBuilderFactory domFactory=DocumentBuilderFactory.newInstance(); 
    domFactory.setValidating(false); 
    domFactory.setNamespaceAware(false); 
    DocumentBuilder domBuilder=domFactory.newDocumentBuilder(); 
    Document document=domBuilder.parse(new File(path));                  //开始分析文件 String[] cond=new String[50]; 
    int i=0; 
    cond[i]="students/JobPart";                                        //寻找到所有JobPart节点 
    NodeList JobParts=XPathAPI.selectNodeList(document,cond[i]); 
    System.out.println(JobParts.getLength()); 
    i++; 
      
    for(int t=0;t <JobParts.getLength();t++){                            //遍历每个JobPart节点 
    Node JobPart=JobParts.item(t); 
    String JobPartID=JobPart.getAttributes().getNamedItem("id").getNodeValue(); 
    System.out.println("JobPart"+JobPartID+":");                  //寻找到每个JobPart节点的id cond[i]="JobPart/contact/@phone"; 
    Node JobContact=XPathAPI.selectSingleNode(JobPart,cond[i]);  //寻找到每个JobPart节点的ContactPhone String JobContactName=JobContact.getNodeName();  //第32行 
    String JobContactValue=JobContact.getNodeValue(); 
    System.out.print(JobContactName+":"); 
    System.out.println(JobContactValue); 
      } 

    catch (Exception e) 
    {e.printStackTrace();} 
      } 

    程序运行结果为: 

    JobPart1: 
    java.lang.NullPointerException 
    at test.main(test.java:32) 其实我只是想在这个XML文件的 两个 <JobPart>.... </JobPart>节点中用XPath去寻找所需信息,我明白在整个XML文件中用XPath去寻找所需信息的方法,但是用相同的方法在里面的某个节点中去寻找信息怎么就不行了呢
      

  11.   

    你的意思是
    String JobPartID=JobPart.getAttributes().getNamedItem("id").getNodeValue(); 
    System.out.println("JobPart"+JobPartID+":"); 这个正确
    可是
    cond[i]="JobPart/contact/@phone"; 
    Node JobContact=XPathAPI.selectSingleNode(JobPart,cond[i]);  //寻找到每个JobPart节点的ContactPhone String JobContactName=JobContact.getNodeName();  //第32行 
    String JobContactValue=JobContact.getNodeValue();却不正确是不是啊?
    不明白你的意思。
    你仔细看看区别,Node JobPart=JobParts.item(t); JobPart 这是个节点他的路径是students/JobPart 中的第t个节点
    也就是说他的内容是第一个节点
    <JobPart id="1"> 
      <name>zhang </name> 
      <sex>female </sex> 
      <contact phone="138000001"> 
      </contact> 
    </JobPart> 
    第二个节点
     <JobPart id="2"> 
      <name>li </name> 
      <sex>male </sex> 
      <contact phone="138000002">  
      </contact> 
    </JobPart>
    所以在
    cond[i]="JobPart/contact/@phone"; 
    Node JobContact=XPathAPI.selectSingleNode(JobPart,cond[i]);  //寻找到每个JobPart节点的ContactPhone String JobContactName=JobContact.getNodeName();  //第32行 
    String JobContactValue=JobContact.getNodeValue(); 
    这个当中路径就变成了cond[i]="JobPart/contact/"; 
    其中@phone和id的位置相同所以取值方法是
    String JobPartID=JobPart.getAttributes().getNamedItem("id").getNodeValue(); 
    和这个一样应该为
    String phone=JobPart.getAttributes().getNamedItem("phone").getNodeValue(); 
    先看看10楼的东西
    是说你应该规范些,路径名不在同等位置的节点尽量不要重复 
      

  12.   

    或许是这样写的cond[i]="JobPart/contact@phone";
      

  13.   

    回复13楼,您好,我按照您的方法做了,结果依然是:
    java.lang.NullPointerException
    at test.main(test.java:32)
    2
    JobPart1:
      

  14.   


    cond[i]="contact"; 
    Node JobContact=XPathAPI.selectSingleNode(JobPart,cond[i]);  //寻找到每个JobPart节点的ContactPhone String JobContactName=JobContact.getAttributes().getNamedItem("phone").getNodeValue();;  //第32行 
    String JobContactValue=JobContact.getNodeValue(); 
    System.out.print(JobContactName+":"); 
    System.out.println(JobContactValue); 
    我操这次再不行,我就去撞墙
      

  15.   

    cond[i]="students/contact/@phone";  
    Node JobContact=XPathAPI.selectSingleNode(JobPart,cond[i]);  //寻找到每个JobPart节点的ContactPhone 改为:
    cond[i]="contact/@phone"; 就可以了
    为什么是这样,楼主可以想想一下,节点关系
      

  16.   

    非常感谢17楼,确实是这样的,可是还要请教您的是,您说的这个节点关系我还是不太明白,为什么在这个时候就不能用cond[i]="students/contact/@phone";  了呢因为我在整个XML文件中查询的时候都是从最高节点开始的,为什么在这里就要从最高节点的下一节点开始了呢,非常感谢!
      

  17.   

    cond[i] = "students/students"; //寻找到所有JobParts节点
    NodeList JobParts = XPathAPI.selectNodeList(document, cond[i]);因为JobParts 返回list 已经是students下的students,所以在下面再进行查找的时候,只要指定第二个students
    下面的就好了如果按你说的从最高节点开始,其实也行,那么你就要改成
    cond[i] = "students/students/contact/@phone";
    Node JobContact = XPathAPI.selectSingleNode(document, cond[i]); //寻找到每个JobPart节点的ContactPhone但是这样没必要,你上面已经找出来了,何必画些没用的时间从头开始了,是吧,呵呵
      

  18.   

    回复hedangqing(~~&)O阳光下的小草~~),谢谢您的讲解,我明白了,真的是茅塞顿开,非常感谢! 
      

  19.   

    根据你这个xml如何跟你第一个的name:zhang来获得第一个的student剩下的内容呢
    求帮忙 Q824890701