开发者

Grabbing XML File Data from a Website

Just wondering what would be the best way to grab the following data and parse it.

Here's an example of some the data I want to pull.

<?xml version="1.0" encoding="UTF-8" ?> 
<eveapi version="2">
    <currentTime>2010-11-19 19:23:44</currentTime> 
    <result>
        <rowset name="characters" key="characterID" columns="name,characterID,corporationName,corporationID">
            <row name="jennyhills" characterID="90052591" corporationName="Imperial Academy" corporationID="1000166" /> 
       </rowset>
    </result>
    <cachedUntil>2010-11-19 20:20:44</cachedUntil> 
</eveapi>

I've seen some examples on how to parse XML data but they are all based on if statements and that's a lot of hard coding is there a more generic way to do开发者_运维技巧 this?


Parsers are quite hardcoded that's the way they work. You can only check if a certain tag matches a certain pattern and then decide what to do. Especially for simple documents like yours that is absolutely no problem.

If you have more than one type of document to parse then I recommend reading this SO answer.


The "parsing", taking the term literally, is easy. Parsing is the process of taking a text string (in your case, from an http response) and turning it into a data structure such as an XML document tree. That process is handled for you by an XML parser, and you typically don't need to worry about it.

The part you're facing is how to query data from the parsed XML document, right? The easiest way to depends greatly on what you need to do with the data. But XPath is a good way to select data without a lot of verbose if statements and get-child function calls.

See also this question on using XPath in Android.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜