-1

Android プロジェクトに取り組んでいますが、ローカル ファイル (raw/file.xml) を解析できません。インターネットで見つけたいくつかのアドバイスを試しましたが、無駄でした。ネットでファイルを解析しようとすると(URLクラスを使用して...)動作しますが、ローカルファイルプログラムを使用しようとすると動作しません!!! 助けてください。(私は XML パーサーとして Sax を使用しています)

sourceUrl = new URL ("example.com/w...";);行の URLを次のように置き換えます。InputSource is = new InputSource(getResources().openRawResource(R.raw.example));

xr.parse(new InputSource(sourceUrl.openStream()));との行xr.parse(new InputSource(is.getByteStream()));

4

1 に答える 1

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;
}
于 2012-07-10T14:32:10.133 に答える