Java android中使用android.sax的XML解析问题

Java android中使用android.sax的XML解析问题,java,android,sax,xml-parsing,Java,Android,Sax,Xml Parsing,我有一个xml文件 <?xml version="1.0" encoding="utf-8"?> <sections> <section> <name>Most Pouplar</name> <items> <item pos="1"> <name> AcaiBerr

我有一个xml文件

<?xml version="1.0" encoding="utf-8"?>
<sections>
    <section>
        <name>Most Pouplar</name>
        <items>
            <item pos="1">
                <name>
                    AcaiBerry Diet
                </name>
                <description>

                    <![CDATA[
                    Natrol AcaiBerry Diet supports weight loss goals when combined with a healthy reduced-calorie diet and exercise program. Acai is a wild fruit harvested in the rain forests of Brazil recognized for its high ORAC (oxygen-radical absorbance capacity) value - a measure of its antioxidant capacity. An adequate intake of antioxidants helps neutralize harmful free radicals that are produced by the body as a result of regular exercise.
                    ]]>
                </description>
            </item>
            <item pos="2">
                <name>
                    AcaiBerry Weekend Cleanse
                </name>
                <description>
                    <![CDATA[
                    AcaiBerry Weekend Cleanse is a 3-step, easy-to-use cleansing program. Step 1 helps minimize occasional constipation/bloating, step 2 helps reduce toxins via antioxidant protection & cell regeneration and step 3 helps to restore the friendly bacteria that protect & strengthen the GI tract.
                    ]]>

                </description>
            </item>
            <item pos="4">
                <name>
                    Carb Intercept Phase 2 + Chromium
                </name>
                <description>
                    <![CDATA[
                    Natrol Carb Intercept supports a low-carb lifestyle by controlling carbohydrates found in breads, cereals, rice, pasta and other starch-containing foods. Each serving provides 1,000mg of Phase 2 Carb Controller; a clinically tested ingredient that inhibits the enzyme responsible for digesting starch into simple sugars your body can absorb.
                    ]]>
                </description>

            </item>
            <item pos="3">
                <name>
                    Resveratrol Diet
                </name>
                <description>
                    <![CDATA[
                    Losing weight has never been so rejuvenating! Natrol introduces Resveratrol Diet, a complex blend of antioxidants, enzymes and other nutrientsto help boost your metabolism and promote calorie burning.
                    ]]>
                </description>
            </item>

        </items>
    </section>    
    <section>
        <name>Least Popular</name>
        <items>
            <item pos="1">
                <name>
                    Advanced Sleep Melatonin 10mg Maximum Strength
                </name>

                <description>
                    <![CDATA[
                    Getting a good night's sleep is even easier with Natrol Melatonin - a natural nightcap. A hormone found in the body, melatonin, helps promote more restful sleep. Natrol Melatonin provides relief for occasional sleeplessness, and helps promote a more relaxing night and better overall health.
                    ]]>
                </description>
            </item>
            <item pos="2">
                <name>
                    Sleep 'N Restore
                </name>
                <description>

                    <![CDATA[
                    If you need to feel more rested due to lack of sleep, try Natrol Sleep 'N Restore. Sleep 'N Restore helps promote a more restful, deeper sleep, while supporting your body's natural restoration processes.* A combination of melatonin and valerian, this natural sleep aide includes antioxidants that can help your body protect its cells from damage to help you restore and recharge while you sleep.
                    ]]>
                </description>
            </item>
        </items>
    </section>    
</sections>
我正在实现一种解析xml文件的方法,但我不知道如何读取
标记中的多个
标记

已编辑

我把代码的一部分,我正在尝试

//Store all items with a particular section 
ArrayList<ItemPojo> itemList = new ArrayList<ItemPojo>();
//Store all items categorized by section 
Map<String, ArrayList<ItemPojo>> itemStore = new HashMap<String, ArrayList<ItemPojo>>(1);
//Single item
ItemPojo currentItem = null;
//Current section name
String sectionName = null;

public AndroidSaxFeedParser() {
    super();
}

public void parse() { //Map<String, ArrayList<ItemPojo>>

    RootElement root = new RootElement(SECTIONS);

    Element section = root.getChild(SECTION);
    Element itemHeader = section.getChild(ITEM_HEADER);



    //Read <name> tag as used as section
    itemHeader.setEndTextElementListener(new EndTextElementListener() {         
        public void end(String body) {
            sectionName = body;
        }
    });
    //TODO set item header here

    Element items = section.getChild(ITEMS);
    Element item = items.getChild(ITEM);

    /*//Put all items of same category
    items.setEndTextElementListener(new EndTextElementListener() {

        public void end(String body) {

            //sort item with position 
            Collections.sort(itemList, ItemPojo.COMPARE_BY_POSITION);
            //Putting it into master list
            itemStore.put(sectionName, itemList);
            //And clear the item list
            itemList.clear();
        }
    });*/

    item.setStartElementListener(new StartElementListener() {           
        public void start(Attributes attributes) {
            currentItem = new ItemPojo();
            Log.i("Test xml", "item initalised " + currentItem.toString());
        }
    });

    item.setEndTextElementListener(new EndTextElementListener() {

        public void end(String body) {
            // TODO Auto-generated method stub
            itemList.add(currentItem);
            Log.i("Test xml", "New items found " + currentItem.toString());
        }
    });

    item.getChild(ITEM_NAME).setEndTextElementListener(new EndTextElementListener() {           
        public void end(String body) {
            currentItem.setItemName(body);
        }
    });

    item.getChild(DESCRIPTION).setEndTextElementListener(new EndTextElementListener() {

        public void end(String body) {
            currentItem.setItemDescription(body);
        }
    });

    try {
        Xml.parse(this.getInputStream(), Xml.Encoding.UTF_8, root.getContentHandler());
    } catch (Exception e) {
        throw new RuntimeException(e);
    }

    //return itemStore;
}
我需要什么


我需要阅读所有项目(包括pos、名称和描述)和章节。我使用一个HashMap,作为我放置部分的键,作为该键的值,我放置一个与该特定键(作为部分名称)相关的所有项(带位置、名称、描述)的数组列表。

这里您似乎没有使用SAX解析器,而是使用DOM traveral

如果希望使用SAX解析器解析XML,则需要初始化SAX解析器并定义ContentHandler,在其中实现解析逻辑

SAXParserFactory spf = SAXParserFactory.newInstance();
SAXParser sp = spf.newSAXParser();
XMLReader xr = sp.getXMLReader();

/** Send URL to parse XML Tags */
URL sourceUrl = new URL(xmlFile);

/** Create handler to handle XML Tags ( extends DefaultHandler ) */
MyXMLHandler myXMLHandler = new MyXMLHandler();
xr.setContentHandler(myXMLHandler);
xr.parse(new InputSource(sourceUrl.openStream()));
ContentHandler在解析器到达开始和结束元素时具有回调。在那里,您可以放置填充POJO所需的逻辑

在您的特定情况下,您需要检查项目标签并开始填充POJO

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

public class MyXMLHandler extends DefaultHandler {

@Override
public void startElement(String uri, String localName, String qName,
Attributes attributes) throws SAXException {

if (localName.equals("sometag")) {
// process tag
}

}

@Override
public void endElement(String uri, String localName, String qName)
throws SAXException {
// handle end element
}
}
网上有几篇文章提供了这方面的完整代码示例。只需谷歌Android和SAX解析器


可以找到一个使用SAX解析XML文件(包含项元素)的示例。

这里似乎没有使用SAX解析器,而是使用DOM traveral

如果希望使用SAX解析器解析XML,则需要初始化SAX解析器并定义ContentHandler,在其中实现解析逻辑

SAXParserFactory spf = SAXParserFactory.newInstance();
SAXParser sp = spf.newSAXParser();
XMLReader xr = sp.getXMLReader();

/** Send URL to parse XML Tags */
URL sourceUrl = new URL(xmlFile);

/** Create handler to handle XML Tags ( extends DefaultHandler ) */
MyXMLHandler myXMLHandler = new MyXMLHandler();
xr.setContentHandler(myXMLHandler);
xr.parse(new InputSource(sourceUrl.openStream()));
ContentHandler在解析器到达开始和结束元素时具有回调。在那里,您可以放置填充POJO所需的逻辑

在您的特定情况下,您需要检查项目标签并开始填充POJO

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

public class MyXMLHandler extends DefaultHandler {

@Override
public void startElement(String uri, String localName, String qName,
Attributes attributes) throws SAXException {

if (localName.equals("sometag")) {
// process tag
}

}

@Override
public void endElement(String uri, String localName, String qName)
throws SAXException {
// handle end element
}
}
网上有几篇文章提供了这方面的完整代码示例。只需谷歌Android和SAX解析器


可以找到一个使用SAX解析XML文件(包含项元素)的示例。

您已经走上了正轨。下一步是:

  • 为项目元素定义startElementListener。 像这样:

    item.setStartElementListener(new StartElementListener() { @Override public void start(Attributes attributes) { myPojoItem = new PojoItem(); } }); item.setEndElementListener(new EndElementListener() { @Override public void end() { itemList.add(myPojoItem); } }); 最终解决方案

    来自OP:我用这些方法完成了它

    公共类AndroidSaxFeedParser扩展了BaseFeedParser{
    //存储具有特定节的所有项目
    ArrayList itemList=新建ArrayList();
    //存储按节分类的所有项目
    Map itemStore=newhashmap(1);
    //单项
    ItemPojo currentItem=null;
    //当前节名称
    字符串sectionName=null;
    公共AndroidSaxFeedParser(){
    超级();
    }
    公共映射解析(){
    根元素根=新根元素(节);
    元素节=root.getChild(节);
    元素itemHeader=section.getChild(ITEM_头);
    //读取标记作为节使用
    itemHeader.setEndTextElementListener(新的EndTextElementListener(){
    公共无效结束(字符串体){
    sectionName=body.trim();
    Log.i(“新章节”,“新章节发现:”+章节名称);
    }
    });
    section.setStartElementListener(新的StartElementListener(){
    公共void开始(属性){
    //清除项目列表
    itemList=新的ArrayList(0);
    Log.i(“列表大小”,“大小:+itemList.Size());
    }
    });
    section.setEndElementListener(新的EndElementListener(){
    公共无效结束(){
    //将其放入主列表中
    itemStore.put(sectionName,itemList);
    }
    });
    元素项=section.getChild(项);
    元素项=items.getChild(item);
    items.setEndElementListener(新的EndElementListener(){
    公共无效结束(){
    //按位置对项目排序
    Collections.sort(itemList、ItemPojo.COMPARE\u BY\u POSITION);
    }
    });     
    item.setStartElementListener(新的StartElementListener(){
    公共void开始(属性){
    currentItem=新的ItemPojo();
    currentItem.setItemPosition(Integer.parseInt(attributes.getValue(“pos”));
    //i(“测试xml”、“项初始化”+currentItem.toString());
    }
    });
    item.setEndElementListener(新的EndElementListener(){
    公共无效结束(){
    itemList.add(当前项);
    i(“测试xml”,“找到新项”+currentItem.toString());
    }
    });
    item.getChild(item_NAME).setEndTextElementListener(新的EndTextElementListener(){
    公共无效结束(字符串体){
    currentItem.setItemName(body.trim());
    }
    });
    item.getChild(DESCRIPTION).setEndTextElementListener(新的EndTextElementListener(){
    公共无效结束(字符串体){
    currentItem.setItemDescription(body.trim());
    }
    });
    试一试{
    parse(this.getInputStream(),Xml.Encoding.UTF_8,root.getContentHandler());
    }捕获(例外e){
    抛出新的运行时异常(e);
    }
    返回项目存储;
    }
    }
    

    公共抽象类BaseFeedParser实现FeedParser{
    //XML标记的名称
    静态最终字符串SECTIONS=“SECTIONS”;
    静态最终字符串SECTION=“SECTION”;
    静态最终字符串项\u HEADER=“name”;
    静态最终字符串DESCRIPTION=“DESCRIPTION”;
    静态最终字符串项\u NAME=“NAME”;
    静态最终字符串项\u POSITION=“pos”;
    静态最终字符串ITEM=“ITEM”;
    静态最终字符串ITEMS=“ITEMS”;
    流内公共输入流;
    公共BaseFeedParser(){
    //超级();
    }
    受保护的InputStream getInputStream(){
    itemName.setEndTextElementListener(new EndTextElementListener() {
                @Override
                public void end(String body) {
                                   myPojoItem.setName(body);
                }
            });
    try {
                Xml.parse(myXmlAsFileInputStream, Xml.Encoding.UTF_8, root.getContentHandler());
            } catch (Exception e) {
                e.printStackTrace();
            }
    
    item.setStartElementListener(new StartElementListener() {
        @Override
        public void start(Attributes attributes) {
            position = attributes.getValue("pos");
        }
    });
    
    AndroidSaxFeedParser.java
    
    public class AndroidSaxFeedParser extends BaseFeedParser {
    
        //Store all items with a particular section 
        ArrayList<ItemPojo> itemList = new ArrayList<ItemPojo>();
        //Store all items categorized by section 
        Map<String, ArrayList<ItemPojo>> itemStore = new HashMap<String, ArrayList<ItemPojo>>(1);
        //Single item
        ItemPojo currentItem = null;
        //Current section name
        String sectionName = null;
    
        public AndroidSaxFeedParser() {
            super();
        }
    
        public Map<String, ArrayList<ItemPojo>> parse() { 
    
            RootElement root = new RootElement(SECTIONS);
    
            Element section = root.getChild(SECTION);
            Element itemHeader = section.getChild(ITEM_HEADER);
    
            //Read <name> tag as used as section
            itemHeader.setEndTextElementListener(new EndTextElementListener() {         
                public void end(String body) {
                    sectionName = body.trim();
                    Log.i("New Section", "New section found : " + sectionName);
                }
            });
    
    
            section.setStartElementListener(new StartElementListener() {            
                public void start(Attributes attributes) {
                    //Clear the item list
                    itemList = new ArrayList<ItemPojo>(0);
                    Log.i("Size of list", "Size : " +itemList.size());
                }
            });
            section.setEndElementListener(new EndElementListener() {            
                public void end() {
                    //Putting it into master list
                    itemStore.put(sectionName, itemList);
                }
            });
    
            Element items = section.getChild(ITEMS);
            Element item = items.getChild(ITEM);
    
            items.setEndElementListener(new EndElementListener() {
    
                public void end() {
                    //sort item with position 
                    Collections.sort(itemList, ItemPojo.COMPARE_BY_POSITION);               
                }
            });     
    
            item.setStartElementListener(new StartElementListener() {
    
                public void start(Attributes attributes) {
                    currentItem = new ItemPojo();
                    currentItem.setItemPosition(Integer.parseInt(attributes.getValue("pos")));
                    //Log.i("Test xml", "item initalised " + currentItem.toString());           
                }
            });
    
            item.setEndElementListener(new EndElementListener() {
    
                public void end() {
    
                    itemList.add(currentItem);
                    Log.i("Test xml", "New items found " + currentItem.toString());
                }
            });
    
            item.getChild(ITEM_NAME).setEndTextElementListener(new EndTextElementListener() {           
                public void end(String body) {
                    currentItem.setItemName(body.trim());
                }
            });
    
            item.getChild(DESCRIPTION).setEndTextElementListener(new EndTextElementListener() {
    
                public void end(String body) {
                    currentItem.setItemDescription(body.trim());
                }
            });
    
            try {
                Xml.parse(this.getInputStream(), Xml.Encoding.UTF_8, root.getContentHandler());
            } catch (Exception e) {
                throw new RuntimeException(e);
            }
    
            return itemStore;
        }
    }
    
    public abstract class BaseFeedParser implements FeedParser {
    
        // names of the XML tags
        static final String SECTIONS = "sections";
        static final String SECTION = "section";
        static final String ITEM_HEADER = "name";
        static final  String DESCRIPTION = "description";
        static final  String ITEM_NAME = "name";
        static final  String ITEM_POSITION = "pos";
        static final  String ITEM = "item";
        static final  String ITEMS = "items";
        public InputStream inStream;
    
        public BaseFeedParser() {
            //super();
        }
    
        protected InputStream getInputStream() {
            //Create a new HttpClient and Post Header
            HttpClient httpclient = new DefaultHttpClient();
            HttpPost httppost = new HttpPost(DesTestDemoActivity.INDEX_URL);
            HttpResponse response = null;
            try {
                // Add your data
                List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
                nameValuePairs.add(new BasicNameValuePair("request_for", "xml_data"));
                httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
                // Execute HTTP Post Request
                response = httpclient.execute(httppost);    
                HttpEntity entity = response.getEntity();
                if (entity != null)
                    inStream = entity.getContent(); 
                return inStream;
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
    }
    
    public interface FeedParser {
        Map<String, ArrayList<ItemPojo>> parse();
    }