SAX parser get attribute from endelement
Asked Answered
I

2

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

Illuminator answered 17/2, 2014 at 6:41 Comment(1)
can you post your code to show what have you tried until now?Kep
D
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
Dinghy answered 17/2, 2014 at 6:46 Comment(16)
I can't do that. I get that file from other app.Illuminator
I have file like that: github.com/designrifts/Ultimate_Theme_UI_Template/blob/master/… but with 1000 lines :PIlluminator
Like this: <item component="ComponentInfo{com.designrifts.ultimatethemeui/ultimatethemeui.themeactivity}" drawable="icon" />Illuminator
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.Dinghy
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.Illuminator
When I use startElement they dont found any item :/Illuminator
Yes, but have more lines.Illuminator
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.Illuminator
how looks XML you parseIlluminator
when I run app still found only in endElements :/Illuminator
So why my parse script found that only in endElements @DinghyIlluminator
I post as answare my current code and that don't work. @DinghyIlluminator
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..Dinghy
Util.l(String.valueOf(SAXXMLHandler.icons.size())); print 0Illuminator
so what do You think?Illuminator
It will work man..see my answer below which is giving your expected output, the problem some where else..check itDinghy
K
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.

Kep answered 17/2, 2014 at 7:18 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.