简体   繁体   中英

Android XML parsing, Documentbuilder throws exception

So, I'm trying to parse an XML file in my Android app using nodelist and documentbuilder. Problem is, Documentbuilder.parse() always returns null and ends up throwing an IOException. I'm guess it has something to do with the filepath being wrong.

public class EquiScoreActivity extends Activity 
{
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        ProtocolFactory.GetProtocolFactory().LoadProtocol("EquiScore/assets/Protocols/LC1_2008.xml");
    }
}

public class ProtocolFactory 
{
    private static ProtocolFactory m_Instance = new ProtocolFactory();
    Document dom;

    private ProtocolFactory()
    {
    }

    public static ProtocolFactory GetProtocolFactory()
    {
        return m_Instance;
    }

    public Protocol LoadProtocol(String filename)
    {
        Protocol output = null;
        List<JudgementGroup> jGroups;
        List<GeneralAssesment> gAssesment;

        ParseXmlFile(filename);
        ParseDocument();

        return output;
    }


    private void ParseXmlFile(String filename)
    {
        //get the factory
        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();

        try 
        {
            //Using factory get an instance of document builder
            DocumentBuilder db = dbf.newDocumentBuilder();

            //parse using builder to get DOM representation of the XML file
            dom = db.parse(filename);
        }
        catch(ParserConfigurationException pce)
        {
            pce.printStackTrace();
        }
        catch(SAXException se) 
        {
            se.printStackTrace();
        }
        catch(IOException ioe) 
        {
            ioe.printStackTrace();
        }
    }

}

Basically I need to know how to get the proper filepath or some other (maybe better?) way to parse the XML. Each node of one type can have differing numbers of child nodes, which is why I figured this approach would be a good one.

The IOException is: "MalformedURLException" "Protocol not found: EquiScore/assets/Protocols/LC1_2008.xml"

I've tried various variations of the filename but seem to get this error anyway.

The IOException you're receiving is due to the fact that what you're passing is a bare pathname while DocumentBuilder.parse() expects a URL.

The first part of the URL is called "protocol" (it's the http in http://www.google.com ). Your pathname doesn't specify a protocol and hence the error reads "Protocol not found". You can make a URL out of a pathname by prepending file:// to it.

Alternatively, you can create a File or FileInputStream object and pass it to DocumentBuilder.parse() since the method is overloaded. For example like this:

dom = db.parse(new File(filename));

EDIT: Since your file is in the assets/ folder you can use AssetManager.open() which returns InputStream which is in turn accepted by one of the overloaded versions of DocumentBuilder.parse() :

AssetManager assetManager = ...
dom = db.parse(assetManager.open("Protocols/LC1_2008.xml"));

Apparently you've placed the XML in the assets folder, thus you should use the AssetManager to retrieve an InputStream to parse.

Something like this:

Context context = ...;
AssetManager assManager = context.getAssets();
DocumentBuilder db = ..;
db.parse(assManager.open("Protocols/LC1_2008.xml"));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM