java中DOM解析XML文件奇葩的错误 java.lang.NullPointerException
DocumentBuilderFactorydbf=DocumentBuilderFactory.newInstance();DocumentBuilderbuilder...
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = dbf.newDocumentBuilder();
Document doc = builder.parse(xmlPath);
Element root = doc.getDocumentElement();
Root = root;
return Root.getElementsByTagName("loginnerserverurl").item(0).getFirstChild()
.getNodeValue();
结果在" .getNodeValue(); "这一行报错java.lang.NullPointerException,也就是说存在<loginnerserverurl>元素,但是元素值为空,元素里面明明有值啊
xml文件中:<loginnerserverurl>http://111.111.1.111:8080/jsp/</loginnerserverurl> 展开
DocumentBuilder builder = dbf.newDocumentBuilder();
Document doc = builder.parse(xmlPath);
Element root = doc.getDocumentElement();
Root = root;
return Root.getElementsByTagName("loginnerserverurl").item(0).getFirstChild()
.getNodeValue();
结果在" .getNodeValue(); "这一行报错java.lang.NullPointerException,也就是说存在<loginnerserverurl>元素,但是元素值为空,元素里面明明有值啊
xml文件中:<loginnerserverurl>http://111.111.1.111:8080/jsp/</loginnerserverurl> 展开
展开全部
代码如下:
public class LoadXml extends Activity {
private final static String TAG="LoadXml";
public static Context context = null;
Document document = null;
NodeList childsNodes = null;
DocumentBuilderFactory factory = null;
DocumentBuilder builder = null;
InputStream inputStreams = null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button button1=(Button)findViewById(R.id.button1);
button1.setOnClickListener(button1OnClickListener);
}
OnClickListener button1OnClickListener=new OnClickListener(){
@Override
public void onClick(View v) {
Log.d(TAG, "onClick");
getComplateXml();
}
};
public void getComplateXml(){
Log.d(TAG, "123");
try {
Log.d(TAG, "getComplateXml");
readUserConfig();
// int j = 0;
// for (int i = 0; i < childsNodes.getLength(); i++) {
// Log.d(TAG, "1");
// Node node = (Node) childsNodes.item(i);
// Log.d(TAG, "2");
// ContentResolver contentResolver = this.context
// .getContentResolver();
// Log.d(TAG, "3");
// Uri insertUri = Uri.parse("content://com.huawei.biz.LoadContentProvider/STUDENT");
// Log.d(TAG, "4");
// ContentValues values = new ContentValues();
// values.put("name", node.getNodeName());
// Log.d(TAG, node.getNodeName());
//
// values.put("id", node.getFirstChild().getNodeValue());
// Log.d(TAG, node.getFirstChild().getNodeValue());
//
// values.put("Photo",DataManager.getDrawableList().get(j));
// Log.d(TAG, DataManager.getDrawableList().get(j).toString());
// contentResolver.insert(insertUri, values);
// j++;
// }
} catch (Exception e) {
e.printStackTrace();
}
}
private void readUserConfig() throws Exception{
Log.d(TAG, "readUserConfig");
Log.d(TAG, "1");
factory = DocumentBuilderFactory.newInstance();
Log.d(TAG, "2");
builder = factory.newDocumentBuilder();
Log.d(TAG, "3");
inputStreams = LoadXml.context.getResources().getAssets().open("student.xml");
Log.d(TAG, "4");
document = builder.parse(inputStreams);
Log.d(TAG, "5");
childsNodes = document.getDocumentElement().getChildNodes();
Log.d(TAG, "6");
}
}
public class LoadXml extends Activity {
private final static String TAG="LoadXml";
public static Context context = null;
Document document = null;
NodeList childsNodes = null;
DocumentBuilderFactory factory = null;
DocumentBuilder builder = null;
InputStream inputStreams = null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button button1=(Button)findViewById(R.id.button1);
button1.setOnClickListener(button1OnClickListener);
}
OnClickListener button1OnClickListener=new OnClickListener(){
@Override
public void onClick(View v) {
Log.d(TAG, "onClick");
getComplateXml();
}
};
public void getComplateXml(){
Log.d(TAG, "123");
try {
Log.d(TAG, "getComplateXml");
readUserConfig();
// int j = 0;
// for (int i = 0; i < childsNodes.getLength(); i++) {
// Log.d(TAG, "1");
// Node node = (Node) childsNodes.item(i);
// Log.d(TAG, "2");
// ContentResolver contentResolver = this.context
// .getContentResolver();
// Log.d(TAG, "3");
// Uri insertUri = Uri.parse("content://com.huawei.biz.LoadContentProvider/STUDENT");
// Log.d(TAG, "4");
// ContentValues values = new ContentValues();
// values.put("name", node.getNodeName());
// Log.d(TAG, node.getNodeName());
//
// values.put("id", node.getFirstChild().getNodeValue());
// Log.d(TAG, node.getFirstChild().getNodeValue());
//
// values.put("Photo",DataManager.getDrawableList().get(j));
// Log.d(TAG, DataManager.getDrawableList().get(j).toString());
// contentResolver.insert(insertUri, values);
// j++;
// }
} catch (Exception e) {
e.printStackTrace();
}
}
private void readUserConfig() throws Exception{
Log.d(TAG, "readUserConfig");
Log.d(TAG, "1");
factory = DocumentBuilderFactory.newInstance();
Log.d(TAG, "2");
builder = factory.newDocumentBuilder();
Log.d(TAG, "3");
inputStreams = LoadXml.context.getResources().getAssets().open("student.xml");
Log.d(TAG, "4");
document = builder.parse(inputStreams);
Log.d(TAG, "5");
childsNodes = document.getDocumentElement().getChildNodes();
Log.d(TAG, "6");
}
}
展开全部
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
public class Test3 {
//ddd.xml
//<loginnerserverurl>http://111.111.1.111:8080/jsp/</loginnerserverurl>
public static void main(String[] args) {
Test3 t = new Test3();
System.out.println(t.parseXml());
}
public String parseXml(){
DocumentBuilderFactory dbf = null;
DocumentBuilder builder = null;
Element root =null;
try {
dbf = DocumentBuilderFactory.newInstance();
builder = dbf.newDocumentBuilder();
Document doc = builder.parse("D:\\ddd.xml");
root = doc.getDocumentElement();
} catch (Exception e) {
e.printStackTrace();
}
return root.getChildNodes().item(0).getNodeValue();
}
}
不知道这样满足不满足你 的需求,可以使用jdom,这个好点
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
<loginnerserverurl>http://111.111.1.111:8080/jsp/</loginnerserverurl>
会不会是敏感字符呢? 楼主写成这样试试:
<loginnerserverurl>
<![CDATA[http://111.111.1.111:8080/jsp/]]>
</loginnerserverurl>
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
为什么要getFirstChild().getNodeValue().不是直接.getNodeValue()吗?
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询