Android에서 Pull 메서드를 사용하여 XML 파일을 해석하는 방법
org.xmlpull.v1.XmlPullParser;
org.xmlpull.v1.XmlPullParserFactory;
final XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
factory.setNamespaceAware(true);
final XmlPullParser parser = factory.newPullParser();
parser.setInput(new StringReader("<author country=\"United States\">James Elliott</author>");
<author>James Elliott</author>
<author></author>
<author/>
<title>What Is Hibernate</title>
<author>James Elliott</author>
<category>Web</category>
        while (eventType != XmlPullParser.END_TAG) {
            switch (eventType) {
            case XmlPullParser.START_TAG:
                tag = parser.getName();
                final String content = parser.nextText();
                Log.e(TAG, tag + ": [" + content + "]");
                eventType = parser.nextTag();
                break;
            default:
                break;
            }
        }
import java.io.IOException;
import java.io.InputStream;
import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;
import org.xmlpull.v1.XmlPullParserFactory;
import android.util.Log;
public class RssPullParser extends RssParser {
    private final String TAG = FeedSettings.GLOBAL_TAG;
    private InputStream mInputStream;
    public RssPullParser(InputStream is) {
        mInputStream = is;
    }
    public void parse() throws ReaderBaseException, XmlPullParserException, IOException {
        if (mInputStream == null) {
            throw new ReaderBaseException("no input source, did you initialize this class correctly?");
        }
        final XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
        factory.setNamespaceAware(true);
        final XmlPullParser parser = factory.newPullParser();
        parser.setInput(mInputStream);
        int eventType = parser.getEventType();
        if (eventType != XmlPullParser.START_DOCUMENT) {
            throw new ReaderBaseException("Not starting with 'start_document'");
        }
        eventType = parseRss(parser);
        if (eventType != XmlPullParser.END_DOCUMENT) {
            throw new ReaderBaseException("not ending with 'end_document', do you finish parsing?");
        }
        if (mInputStream != null) {
            mInputStream.close();
        } else {
            Log.e(TAG, "inputstream is null, XmlPullParser closed it??");
        }
    }
    /**
     * Parsing the Xml document. Current type must be Start_Document.
     * After calling this, Parser is positioned at END_DOCUMENT.
     * @param parser
     * @return event end_document
     * @throws XmlPullParserException
     * @throws ReaderBaseException
     * @throws IOException
     */
    private int parseRss(XmlPullParser parser) throws XmlPullParserException, ReaderBaseException, IOException {
        int eventType = parser.getEventType();
        if (eventType != XmlPullParser.START_DOCUMENT) {
            throw new ReaderBaseException("not starting with 'start_document', is this a new document?");
        }
        Log.e(TAG, "starting document, are you aware of that!");
        eventType = parser.next();
        while (eventType != XmlPullParser.END_DOCUMENT) {
            switch (eventType) {
            case XmlPullParser.START_TAG: {
                Log.e(TAG, "start tag: '" + parser.getName() + "'");
                final String tagName = parser.getName();
                if (tagName.equals(RssFeed.TAG_RSS)) {
                    Log.e(TAG, "starting an RSS feed <<");
                    final int attrSize = parser.getAttributeCount();
                    for (int i = 0; i < attrSize; i++) {
                        Log.e(TAG, "attr '" + parser.getAttributeName(i) + "=" + parser.getAttributeValue(i) + "'");
                    }
                } else if (tagName.equals(RssFeed.TAG_CHANNEL)) {
                    Log.e(TAG, "\tstarting an Channel <<");
                    parseChannel(parser);
                }
                break;
            }
            case XmlPullParser.END_TAG: {
                Log.e(TAG, "end tag: '" + parser.getName() + "'");
                final String tagName = parser.getName();
                if (tagName.equals(RssFeed.TAG_RSS)) {
                    Log.e(TAG, ">> edning an RSS feed");
                } else if (tagName.equals(RssFeed.TAG_CHANNEL)) {
                    Log.e(TAG, "\t>> ending an Channel");     
                }
                break;
            }
            default:
                break;
            }
            eventType = parser.next();
        }
        Log.e(TAG, "end of document, it is over");
        return parser.getEventType();
    }
    /**
     * Parse a channel. MUST be start tag of an channel, otherwise exception thrown.
     * Param XmlPullParser
     * After calling this function, parser is positioned at END_TAG of Channel.
     * return end tag of a channel
     * @throws XmlPullParserException
     * @throws ReaderBaseException
     * @throws IOException
     */
    private int parseChannel(XmlPullParser parser) throws XmlPullParserException, ReaderBaseException, IOException {
        int eventType = parser.getEventType();
        String tagName = parser.getName();
        if (eventType != XmlPullParser.START_TAG || !RssFeed.TAG_CHANNEL.equals(tagName)) {
            throw new ReaderBaseException("not start with 'start tag', is this a start of a channel?");
        }
        Log.e(TAG, "\tstarting " + tagName);
        eventType = parser.nextTag();
        while (eventType != XmlPullParser.END_TAG) {
            switch (eventType) {
            case XmlPullParser.START_TAG: {
                final String tag = parser.getName();
                if (tag.equals(RssFeed.TAG_IMAGE)) {
                    parseImage(parser);
                } else if (tag.equals(RssFeed.TAG_ITEM)) {
                    parseItem(parser);
                } else {
                    final String content = parser.nextText();
                    Log.e(TAG, tag + ": [" + content + "]");
                }
                // now it SHOULD be at END_TAG, ensure it
                if (parser.getEventType() != XmlPullParser.END_TAG) {
                    throw new ReaderBaseException("not ending with 'end tag', did you finish parsing sub item?");
                }
                eventType = parser.nextTag();
                break;
            }
            default:
                break;
            }
        }
        Log.e(TAG, "\tending " + parser.getName());
        return parser.getEventType();
    }
    /**
     * Parse image in a channel.
     * Precondition: position must be at START_TAG and tag MUST be 'image'
     * Postcondition: position is END_TAG of '/image'
     * @throws IOException
     * @throws XmlPullParserException
     * @throws ReaderBaseException
     */
    private int parseImage(XmlPullParser parser) throws XmlPullParserException, IOException, ReaderBaseException {
        int eventType = parser.getEventType();
        String tag = parser.getName();
        if (eventType != XmlPullParser.START_TAG || !RssFeed.TAG_IMAGE.equals(tag)) {
            throw new ReaderBaseException("not start with 'start tag', is this a start of an image?");
        }
        Log.e(TAG, "\t\tstarting image " + tag);
        eventType = parser.nextTag();
        while (eventType != XmlPullParser.END_TAG) {
            switch (eventType) {
            case XmlPullParser.START_TAG:
                tag = parser.getName();
                Log.e(TAG, tag + ": [" + parser.nextText() + "]");
                // now it SHOULD be at END_TAG, ensure it
                if (parser.getEventType() != XmlPullParser.END_TAG) {
                    throw new ReaderBaseException("not ending with 'end tag', did you finish parsing sub item?");
                }
                eventType = parser.nextTag();
                break;
            default:
                break;
            }
        }
        Log.e(TAG, "\t\tending image " + parser.getName());
        return parser.getEventType();
    }
    /**
     * Parse an item in a channel.
     * Precondition: position must be at START_TAG and tag MUST be 'item'
     * Postcondition: position is END_TAG of '/item'
     * @throws IOException
     * @throws XmlPullParserException
     * @throws ReaderBaseException
     */
    private int parseItem(XmlPullParser parser) throws XmlPullParserException, IOException, ReaderBaseException {
        int eventType = parser.getEventType();
        String tag = parser.getName();
        if (eventType != XmlPullParser.START_TAG || !RssFeed.TAG_ITEM.equals(tag)) {
            throw new ReaderBaseException("not start with 'start tag', is this a start of an item?");
        }
        Log.e(TAG, "\t\tstarting " + tag);
        eventType = parser.nextTag();
        while (eventType != XmlPullParser.END_TAG) {
            switch (eventType) {
            case XmlPullParser.START_TAG:
                tag = parser.getName();
                final String content = parser.nextText();
                Log.e(TAG, tag + ": [" + content + "]");
                // now it SHOULD be at END_TAG, ensure it
                if (parser.getEventType() != XmlPullParser.END_TAG) {
                    throw new ReaderBaseException("not ending with 'end tag', did you finish parsing sub item?");
                }
                eventType = parser.nextTag();
                break;
            default:
                break;
            }
        }
        Log.e(TAG, "\t\tending " + parser.getName());
        return parser.getEventType();
    }
}
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
XML이란 무엇입니까?이것은 저장, 검색 및 공유할 수 있는 형식으로 데이터를 저장하는 강력한 방법입니다. 가장 중요한 것은 XML의 기본 형식이 표준화되어 있기 때문에 시스템이나 플랫폼 간에 로컬 또는 인터넷을 통해 XML을 공유하거나...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.