java用dom解析xml报错

Python024

java用dom解析xml报错,第1张

Exception in thread "main" java.io.FileNotFoundException: D:\MYECLIPSE工作夹\xml\src\book.xm

.parse("D:\\MYECLIPSE工作夹\\xml\\src\\xml\\book.xml")

哥们你仔细对一下这两个路径。

放在D盘下自己先测试吧

祝你成功

代码如下:

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 = 0i <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")

}

}

你没有把class.xml贴出来,但是根据你的代码,我估计你的class.xml是这样的:

.....

<student>

      <name>杨过</name>

</student>

.......

这里name是student里的一个element,而不是一个attribute。所以用getAttribute获取不到.

如果你改成这样的xml:

....

<student name="yangguo"></student>

....

那么name就是student的一个attribute。