源码如下:
public class XmlResolver
{
//****** 全局静态函数 ******
private static DocumentBuilder builder = null;
//-------------------------------------------------------------------------------
public static void Initialize()
{
try{
builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
}catch (ParserConfigurationException e) {e.printStackTrace();Log.d("Fox", "ParserConfigurationException");}
}
//-------------------------------------------------------------------------------
//****** 实例化的定义 ******
private Document doc = null;
//-------------------------------------------------------------------------------
public XmlResolver(String XmlStr)
{//构造函数,读入XML文档
try{//将XML字符串推入doc
doc = builder.parse(new InputSource(new StringReader(XmlStr)));
}catch (Exception e) {return ;}
}
//-------------------------------------------------------------------------------
public XmlNode getRoot()
{//定位到第一个有效的节点(root)
Node Root = null;
try{
Root = doc.getFirstChild();
while(Root.getNodeType()!=1)
Root = Root.getNextSibling();
}catch (Exception e) {return null;}
return new XmlNode(Root);
}
//-------------------------------------------------------------------------------
}
我在该类的构造方法里传入一个xmlString字符串,然后调用doc.getRoot()报空指针,debug一看doc=null,但是builder和xmlString都拿到了呀?
求各位大神赐教
你讲xml 文件传入解析试一下 doc = builder.parse(file); 看看是否还为空,
你还可以在 catch 中 加上 e.getCause().printStackTrace(); 看看错误信息是什么
"<?xml version=\"1.0\" encoding=\"utf-8\"?>"+
""+
"41895"+
"14"+
"张玉英"+
"1"+
"2"+
"1945-06-05 00:00:00.000"+
"440321450605302"+
"1408"+
"深圳市宝安区石岩镇科教卫办公室"+
"13600192600"+
"廖文钊"+
"石岩街道河滨北路10号"+
"27612715"+
"201"+
"201"+
"NK08"+
"2008-11-13 00:00:00.000"+
"2009-05-20 11:00:16.000"+
"1020"+
"2"+
"2"+
"1279"+
"1397"+
"1397"+
""+
""+
"1384"+
"40000"+
"41729.28"+
"1"+
"1"+
"1"+
"1"+
"10"+
"10"+
"10"+
"10"+
""+
""+
"1"+
"1"+
"1"+
"1"+
"10"+
"10"+
"10"+
"10"+
"tm"+
"sf"+
"df"+
"dt"+
"";