日期:2014-05-20  浏览次数:20686 次

请问怎么读XML文件
<?xml version="1.0" encoding="GB2312"?> 
<WEBERP> 
  <FORM> 
  <ACCEPT> 
  <PARAM id="1"> 
  <ITEM> 001 </ITEM> 
  <DBITEM> Product_No </DBITEM> 
  <TYPE> string </TYPE> 
  <MAXLEN> 16 </MAXLEN> 
  </PARAM> 
  </ACCEPT> 
  </FORM> 
  <FORM> 
  <INPUT> 
  <PARAM id="2"> 
  <ITEM> 002 </ITEM> 
  <DBITEM> Product_No </DBITEM> 
  <TYPE> string </TYPE> 
  <MAXLEN> 16 </MAXLEN> 
  </PARAM> 
  <TABLENAME> AcceptInput </TABLENAME> 
  </INPUT> 
  </FORM> 
  <SHEET> 
  </SHEET> 
</WEBERP> 
给两个参数分别是FORM用来区分是读FORM还是读SHEET内容;ACCEPT是下一级的去区分是读ACCEPT还是读INPUT的. 
能给段代码吗,谢谢.

------解决方案--------------------
DocumentBuilder dombuilder = domfac.newDocumentBuilder(); 
InputStream is = new FileInputStream(filename); 
Document doc = dombuilder.parse(is);
Element root = doc.getDocumentElement(); 
NodeList books = root.getChildNodes(); 
if (books != null) 
{
for(int i = 0; i < books.getLength(); i++) 
{
Node book = books.item(i); 
if (book.getNodeName().equals(kind)) 
{
for (Node node = book.getFirstChild(); node != null; node = node.getNextSibling()) 

if (node.getNodeName().equals(formName))
{
for (Node node1 = node.getFirstChild(); node1 != null; node1 = node1.getNextSibling()) 
{
if (node1.getNodeName().equals("PARAM"))
{
for (Node node2 = node1.getFirstChild(); node2 != null; node2 = node2.getNextSibling()) 
{
}
}
else if(node1.getNodeName().equals("TABLENAME"))
{

}
}
}

}
else//sheet
{
 
}

}