原文件:
<?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:
这是怎么回事呢
<?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:
这是怎么回事呢
原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去寻找所需信息的方法,但是用相同的方法在里面的某个节点中去寻找信息怎么就不行了呢,谢谢高手帮忙啊!
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();} } }
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();} } }
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
JobPart/contact 上个
cond[i]="students/contact/@phone";
这是查询条件,查询的是
<contact phone="138000001">
</contact>
其中的属性节点phone,如果换成在整个XML文件中查询,也就是
Node JobContact=XPathAPI.selectSingleNode(document,cond[i]);
就可以查询出来,但是在JobPart中就查询不到
<?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>
cond[i]="students/contact/@phone"; 这是查询条件,
查询的是
<contact phone="138000001">
</contact> 其中的属性节点phone,
如果换成在整个XML文件中查询,也就是 Node JobContact=XPathAPI.selectSingleNode(document,cond[i]); 就可以查询出来,但是在JobPart中就查询不到
原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去寻找所需信息的方法,但是用相同的方法在里面的某个节点中去寻找信息怎么就不行了呢
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楼的东西
是说你应该规范些,路径名不在同等位置的节点尽量不要重复
java.lang.NullPointerException
at test.main(test.java:32)
2
JobPart1:
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);
我操这次再不行,我就去撞墙
Node JobContact=XPathAPI.selectSingleNode(JobPart,cond[i]); //寻找到每个JobPart节点的ContactPhone 改为:
cond[i]="contact/@phone"; 就可以了
为什么是这样,楼主可以想想一下,节点关系
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但是这样没必要,你上面已经找出来了,何必画些没用的时间从头开始了,是吧,呵呵
求帮忙 Q824890701