Question

I am using XMLPullParser in java to parse one xml file with lots of nested child node as follows.

<pt>
   <a>85</a>
   <B>03</B>
     <pd>
         <g>
            <most>155</most>
         </g>
        <e>407</e>
     </pd>
</pt>

I am not much familiar with the api. So using lots of next() method on XmlPullParser instance which is really poor to find one child node like "most". Could someone please help me to search child node in more efficient way using XMLPullParser.

Was it helpful?

Solution

If you are looking for a more efficient way to use XmlPullParser to do the parsing, I'm afraid there isn't any. In JavaScript for example, you can "directly" access an element by specifying its ID, which seems a lot more efficient. But in that case, your browser has already parsed the whole DOM tree, and marked the IDs for future use.

From what I understand, XmlPullParser does the parsing "on the fly", which means that when you reach your target node using "next()" methods, it is the first time it is being read. Thus, there is no other way to access it directly. Also, I believe the XmlPullParser interface does not define any method to jump to some section of a document.

However, if you really need to improve performance, you might be able to pre-process your xml file before parsing it, by removing some parts of the file that should not be useful for example. Depending on what you do, pre-processing plain text can be a lot faster than XML parsing; but there also is a good chance you will break your document by doing so.

From my experience, even though XML has a lot of advantages, its parsing can be really slow. If performance is really critical and if you can, switching to another file format (binary for example) can be a lot faster.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top