我正在开发一个android项目,我无法解析本地文件(raw / file.xml)。我已经尝试过一些我在互联网上找到但却徒劳无功的建议。 当我尝试在网络中解析文件(使用URL类...)时,它工作,但当我尝试使用本地文件程序不起作用!我需要帮助。 (我使用Sax作为XML Parser)
我将行网址sourceUrl = new URL ("example.com/w...";);
替换为InputSource is = new InputSource(getResources().openRawResource(R.raw.example));
以及xr.parse(new InputSource(sourceUrl.openStream()));
xr.parse(new InputSource(is.getByteStream()));
答案 0 :(得分:0)
You can put the xml file path here and keep parsing the inner nodes usiln self running loop. Save the values in arrays accordingly for each node..
NodeList eNodeList = null;
eNodeList = Utils.getXMLFromFilePath(your.xml", "firstNOdeNAme", eNodeList);
getElementsByNodelist(eNodeList)
private static getElementsByNodelist(Nodelist nodelist)
{
if(nodelist!=null)
{
for(int i = 0;i<nodelist.getLength();i++)
{
Element element = (Element) nodelist.item(i);
String id=getTextValue(element, "id");
//For attribute value
element.getAttribute("attributename");
// if u want to parse tjis element again
NodeList l=element.getElementsByTagName("innerTag");
// self loop
getElementsByNodelist(l);
}
}
}
public static String getTextValue(Element ele, String tagName) {
String textVal = "";
NodeList nl = ele.getElementsByTagName(tagName);
if(nl != null && nl.getLength() > 0) {
Element el = (Element)nl.item(0);
if(el.getFirstChild()!=null)
{
textVal = el.getFirstChild().getNodeValue();
}
}
return textVal;
}