import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;
void ReadXML()
{
DocumentBuilderFactory docBuilderFactory = null;
DocumentBuilder docBuilder = null;
Document doc = null;
try {
docBuilderFactory = DocumentBuilderFactory.newInstance();
docBuilder = docBuilderFactory.newDocumentBuilder();
//xml file 放到 assets目录中的
doc = docBuilder.parse(getResources().getAssets().open(”weather.xml”));
//root element
Element root = doc.getDocumentElement();
//Do something here
//get a NodeList by tagname
NodeList nodeList = root.getElementsByTagName(”tag”);
for(int i =0;i< nodeList.getLength();i++)
{
Node nd = nodeList.item(i);
//Read Node
}
} catch (IOException e) {
} catch (SAXException e) {
} catch (ParserConfigurationException e) {
} finally {
doc = null;
docBuilder = null;
docBuilderFactory = null;
}
}

相关文章:

  • 2021-05-15
  • 2022-12-23
  • 2021-12-06
  • 2021-05-04
  • 2021-07-31
  • 2022-12-23
  • 2022-12-23
  • 2021-12-06
猜你喜欢
  • 2022-12-23
  • 2021-12-06
  • 2021-07-27
  • 2022-01-05
  • 2022-12-23
  • 2021-11-22
  • 2021-12-06
相关资源
相似解决方案