6

I use SAX XML Parser and when I use:

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

I can get attributes. But I need get attributes from public void endElement

To parse something like that:

<item name="test" value="somedata" />

Code:

public class SAXXMLHandler extends DefaultHandler {

private ArrayList<itemsList> items;
private String tempVal;
private itemsList tempEmp;

private PackageManager manager;
private String packName;

public SAXXMLHandler(PackageManager manager, String packName) {
    items = new ArrayList<itemsList>();

    this.manager = manager;
    this.packName = packName;

}

public void characters(char[] ch, int start, int length)
        throws SAXException {
    tempVal = new String(ch, start, length);
}

// Event Handlers
public void startElement(String uri, String localName, String qName,
        Attributes attributes) throws SAXException {
    Log.d("INFO", "startElement " + localName + ", " + qName + ", " + attributes);
    // reset
    tempVal = "";
    if (qName.equalsIgnoreCase("item")) {
        // create a new instance of employee
        tempEmp = new itemsList();
        tempEmp.setName(attributes.getValue("name"));
    }
}

public void endElement(String uri, String localName, String qName)
        throws SAXException {
    Log.d("INFO", "endElement " + localName + ", " + qName);

}

And don't logcat from startElement

UPDATE

I use in Fragment:

SAXXMLHandler handler = new SAXXMLHandler();
            SAXParserFactory factory = SAXParserFactory.newInstance();
            SAXParser saxParser = factory.newSAXParser();
            saxParser.parse(asset, handler);
            items = SAXXMLHandler.icons;

            Util.l(String.valueOf(SAXXMLHandler.icons.size())); //log
            for(itemList item:SAXXMLHandler.icons)
            {
                Util.l(item.getComponent()+"\t\t"+item.getComponent()); //log
            }

SAXXMLHandler look:

public class SAXXMLHandler extends DefaultHandler {

    public static ArrayList<itemsList> items;
    private itemsList item;

    public SAXXMLHandler() {
        items = new ArrayList<itemsList>();
    }

    public void characters(char[] ch, int start, int length)
            throws SAXException {

    }

    public void startElement(String uri, String localName, String qName,
            Attributes attributes) throws SAXException {
        item = new itemsList();
        Log.d("INFO", "startElement " + localName + ", " + qName);
        if (qName.equalsIgnoreCase("item")) {
            item.setComponent(attributes.getValue("component"));
            items.add(item);
        }
    }

    public void endElement(String uri, String localName, String qName)
            throws SAXException {

    }
}

And still nothing :/

XML file in other app which I parse http://pastebin.com/5GEthfmU

drozdzynski
  • 1,239
  • 2
  • 13
  • 17

2 Answers2

1

Change System.out.println to ur Log.ins ..

Item .java

package com.rofl;

public class Item {

    private String component;

    private String  drawable;

    public String getComponent() {
        return component;
    }

    public void setComponent(String component) {
        this.component = component;
    }

    public String getDrawable() {
        return drawable;
    }

    public void setDrawable(String drawable) {
        this.drawable = drawable;
    }



}

SAXXMLHandler .java

    package com.rofl;

import java.util.ArrayList;
import java.util.List;

import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;

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

public class SAXXMLHandler extends DefaultHandler {



    public static void main(String argv[]) {

        try {

            SAXParserFactory factory = SAXParserFactory.newInstance();
            SAXParser saxParser = factory.newSAXParser();
            SAXXMLHandler handler = new SAXXMLHandler();
            saxParser.parse("src/file.xml", handler);
            System.out.println(SAXXMLHandler.itemList.size());
            for(Item item:itemList)
            {
                System.out.println(item.getComponent()+"\t\t"+item.getDrawable());
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    public static List<Item> itemList = new ArrayList<Item>();

    private Item item;

    public SAXXMLHandler() {
        itemList = new ArrayList<Item>();

    }

    public void characters(char[] ch, int start, int length)
            throws SAXException {

    }

    public void startElement(String uri, String localName, String qName,
            Attributes attributes) throws SAXException {
        item = new Item();
        if (qName.equalsIgnoreCase("item")) {
            item.setComponent(attributes.getValue("component"));
            item.setDrawable(attributes.getValue("drawable"));
            itemList.add(item);

        }
    }

    public void endElement(String uri, String localName, String qName)
            throws SAXException {

    }
}

output will be:-

8
ComponentInfo{com.designrifts.ultimatethemeui/ultimatethemeui.themeactivity}        icon
ComponentInfo{com.chrislacy.actionlauncher.pro/com.chrislacy.launcher.Launcher}     apps_actionlauncherpro
ComponentInfo{com.teslacoilsw.launcher/com.android.launcher2.Launcher}      apps_novalauncher
ComponentInfo{com.teslacoilsw.launcher.prime/.NovaLauncherPrimeActivity}        apps_novalauncher
ComponentInfo{com.anddoes.launcher/com.anddoes.launcher.Launcher}       apps_apexlauncher
ComponentInfo{com.anddoes.launcher.pro/com.anddoes.launcher.pro.ApexLauncherProActivity}        apps_apexlauncher
ComponentInfo{org.adw.launcher/org.adw.launcherlib.Launcher}        apps_adwlauncher
ComponentInfo{org.adwfreak.launcher/org.adw.launcherlib.Launcher}       apps_adwex
Naren
  • 1,447
  • 1
  • 10
  • 11
  • I can't do that. I get that file from other app. – drozdzynski Feb 17 '14 at 06:58
  • I have file like that: https://github.com/designrifts/Ultimate_Theme_UI_Template/blob/master/res/xml/appfilter.xml but with 1000 lines :P – drozdzynski Feb 17 '14 at 07:03
  • 1
    Dude , then you can read it from startElement method only normally what is the need of getting the attributes from endElement method there..According to SAX parser When a start tag or end tag is encountered, the name of the tag is passed as a String to the startElement or the endElement method, as appropriate. When a start tag is encountered, any attributes it defines are also passed in an Attributes list. Characters found within the element are passed as an array of characters, along with the number of characters (length) and an offset into the array that points to the first character. – Naren Feb 17 '14 at 07:12
  • `public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException { Log.d("INFO", startElement " + localName + ", " + qName + ", " + attributes); }` That don't work. Don't logcat anything. – drozdzynski Feb 17 '14 at 07:16
  • When I use startElement they dont found any item :/ – drozdzynski Feb 17 '14 at 07:30
  • Yes, but have more lines. – drozdzynski Feb 17 '14 at 08:12
  • When I log in endElement I have all items but without attributes, but when I log in startElement I don't have any items in logcat. – drozdzynski Feb 17 '14 at 09:11
  • how looks XML you parse – drozdzynski Feb 18 '14 at 11:20
  • when I run app still found only in endElements :/ – drozdzynski Feb 18 '14 at 11:52
  • So why my parse script found that only in endElements @Naren – drozdzynski Feb 18 '14 at 12:11
  • I post as answare my current code and that don't work. @Naren – drozdzynski Feb 18 '14 at 12:16
  • ok @KrisGroove...What it is printing Util.l(String.valueOf(SAXXMLHandler.icons.size()));?? any Exception ?? Could you please delete all above comments they are unnecessary.. – Naren Feb 18 '14 at 12:34
  • Util.l(String.valueOf(SAXXMLHandler.icons.size())); print 0 – drozdzynski Feb 18 '14 at 17:24
  • so what do You think? – drozdzynski Feb 20 '14 at 19:38
  • It will work man..see my answer below which is giving your expected output, the problem some where else..check it – Naren Feb 21 '14 at 06:14
1

In the given XML line..

<item name="test" value="somedata" />

name and value are attributes which only can be retrieved in startElement() method. Because, Attributes attributes parameter is only passed into startElement(String uri, String localName, String qName, Attributes attributes) method. If you look at endElement(String uri, String localName, String qName) method there has no Attributes attributes. That's why you can't retrieve any attribute from endElement() method. So, if you want to retrieve any attributes from a XML then you have to retrieve them inside startElement(String uri, String localName, String qName, Attributes attributes) method.

Hamid Shatu
  • 9,664
  • 4
  • 30
  • 41