Skip to content Skip to sidebar Skip to footer

Trouble With Xml Parsing Through Java

I am trying to parse xml through JAVA but after parsing I get org.apache.harmony.xml.dom.DocumentImpl@418b4c98. Here is the XML what I am tring to parse, for example, i need id dat

Solution 1:

Using XmlPullParser following the docs http://developer.android.com/training/basics/network-ops/xml.html

Copied xml to assests folder to parser locally ( for testing only ). You can get the xml from the url and parse.

 InputStream is = MainActivity.this.getResources()
                     .getAssets().open("xmlparser.xml");
               new parserPull(is);

Then to parse

publicclassparserPull
{

    privatestaticfinalStringns=null;
    publicparserPull(InputStream open) {
        try
        {
             XmlPullParserparser= Xml.newPullParser();
             parser.setFeature(XmlPullParser.FEATURE_PROCESS_NAMESPACES, false);
             parser.setInput(open, null);
             parser.nextTag();
             List<Entry> all = readFeed(parser);
             for(int i=0;i<all.size();i++)
             {
             Log.i("ID is..........",all.get(i).id);
             Log.i("Link is........",all.get(i).link);
             Log.i("Price is.......",all.get(i).price);
             }
        }catch(Exception e)
        {
            e.printStackTrace();
        }
    }
    private List<Entry> readFeed(XmlPullParser parser)throws XmlPullParserException, IOException {
         List<Entry> entry = null;
        parser.require(XmlPullParser.START_TAG, ns, "prestashop");
        while (parser.next() != XmlPullParser.END_TAG) {
            if (parser.getEventType() != XmlPullParser.START_TAG) {
                continue;
            }
            Stringname= parser.getName();
            //Log.i("..................",name);// Starts by looking for the prestashop tagif (name.equals("products")) {
              entry= readProducts(parser);
            } else {
                skip(parser);
            }
        }  
        return entry;
    }
    private List<Entry> readProducts(XmlPullParser parser)throws XmlPullParserException, IOException {
        List<Entry> entries = newArrayList<Entry>();

        parser.require(XmlPullParser.START_TAG, ns, "products");
        while (parser.next() != XmlPullParser.END_TAG) {
            if (parser.getEventType() != XmlPullParser.START_TAG) {
                continue;
            }
            Stringname= parser.getName();
           // Log.i("..................",name);// Starts by looking for the products tagif (name.equals("product")) {
                entries.add(readEntry(parser));
            } else {
                skip(parser);
            }
        }  
        return entries;
    }
    private Entry readEntry(XmlPullParser parser)throws XmlPullParserException, IOException {
        parser.require(XmlPullParser.START_TAG, ns, "product");
        Stringtitle=null;
        Stringsummary=null;
        Stringlink=null;
        while (parser.next() != XmlPullParser.END_TAG) {
            if (parser.getEventType() != XmlPullParser.START_TAG) {
                continue;
            }
            Stringname= parser.getName();
           // Log.i("...................",name);if (name.equals("id")) {
                title = readId(parser);
            } elseif (name.equals("id_default_image")) {
                summary = readLink(parser);
            } elseif (name.equals("price")) {
                link = readPrice(parser);
            } else {
                skip(parser);
            }
        }
        returnnewEntry(title, summary, link);
    }
    private String readPrice(XmlPullParser parser)throws IOException, XmlPullParserException {
        parser.require(XmlPullParser.START_TAG, ns, "price");
        Stringsummary= readText(parser);
        parser.require(XmlPullParser.END_TAG, ns, "price");
        return summary;
    }
    private String readLink(XmlPullParser parser)throws IOException, XmlPullParserException {
        Stringlink="";
        parser.require(XmlPullParser.START_TAG, ns, "id_default_image");
        Stringtag= parser.getName();
       // Log.i("............",tag);StringrelType= parser.getAttributeValue(null, "not_filterable");  
        if (tag.equals("id_default_image")) {
            if (relType.equals("true")){
                link = parser.getAttributeValue(null, "xlink:href");
                parser.nextTag();
            } 
        }
        parser.require(XmlPullParser.END_TAG, ns, "id_default_image");
        return link;
    }
    private String readId(XmlPullParser parser)throws IOException, XmlPullParserException {
        parser.require(XmlPullParser.START_TAG, ns, "id");
        Stringtitle= readText(parser);
        parser.require(XmlPullParser.END_TAG, ns, "id");
        return title;
    }
    private String readText(XmlPullParser parser)throws IOException, XmlPullParserException {
        Stringresult="";
        if (parser.next() == XmlPullParser.TEXT) {
            result = parser.getText();
            parser.nextTag();
        }
        return result;
    }
    privatevoidskip(XmlPullParser parser)throws XmlPullParserException, IOException {
        if (parser.getEventType() != XmlPullParser.START_TAG) {
            thrownewIllegalStateException();
        }
        intdepth=1;
        while (depth != 0) {
            switch (parser.next()) {
            case XmlPullParser.END_TAG:
                depth--;
                break;
            case XmlPullParser.START_TAG:
                depth++;
                break;
            }
        }
     }
    publicstaticclassEntry {
        publicfinal String id;
        publicfinal String link;
        publicfinal String price;

        privateEntry(String id, String link, String price) {
            this.id = id;
            this.link = link;
            this.price = price;
        }
    }
} 

The log output

12-1003:29:44.664: I/ID is..........(1511):  512-1003:29:44.664: I/Link is........(1511): https://www.10ngah.com/api/images/products/5/512-1003:29:44.674: I/Price is.......(1511):  52512-1003:29:44.674: I/ID is..........(1511):  612-1003:29:44.674: I/Link is........(1511): https://www.10ngah.com/api/images/products/6/612-1003:29:44.674: I/Price is.......(1511):  525

Solution 2:

First your example has syntax error (missing space before attribute)...

I like a JAXB very much... so I would recomend it to you. If your data has static format (not change format in time) it is the best way. Only "problem" could be with CDATA blocks, see this post for more information. Here is "fast" example... it is not nice code just a example!

publicclassAdapterCDATAextendsXmlAdapter {


      @Overridepublic String marshal(String arg0)throws Exception {
        return"";
      }

      @Overridepublic String unmarshal(String arg0)throws Exception {
        return arg0;
      }
    }



    public classTestData {

      @XmlRootElement(name = "prestashop")      @XmlAccessorType(XmlAccessType.FIELD)
      static classPrestashop {

        @XmlElementWrapper(name = "products")        @XmlElement(name = "product")List products;
      }

      @XmlAccessorType(XmlAccessType.FIELD)
      static classProduct {
        @XmlJavaTypeAdapter(AdapterCDATA.class)        @XmlElement(name = "id")
        String id;

        @XmlElement(name = "id_default_image")
        IdDefaultImage idDefaultImage;

        @XmlJavaTypeAdapter(AdapterCDATA.class)        @XmlElement(name = "price")
        String price;

        @XmlElement(name = "name")
        Name name;
      }

      @XmlAccessorType(XmlAccessType.FIELD)
      static classIdDefaultImage {
        @XmlAttribute(name = "not_filterable")
        String notFilterable;

        @XmlAttribute(name = "href", namespace = "http://www.w3.org/1999/xlink")
        String href;

        @XmlJavaTypeAdapter(AdapterCDATA.class)        @XmlValue
        String idDefaultImage;
      }

      @XmlAccessorType(XmlAccessType.FIELD)
      static className {
        @XmlElement(name = "language")
        Language language;
      }

      @XmlAccessorType(XmlAccessType.FIELD)
      static classLanguage {
        @XmlJavaTypeAdapter(AdapterCDATA.class)        @XmlValue
        String language;

        @XmlAttribute(name = "href", namespace = "http://www.w3.org/1999/xlink")
        String href;

        @XmlAttribute(name = "id")
        String id;
      }

      public static void main(String[] args) throws JAXBException {
        JAXBContext jc = JAXBContext.newInstance(Prestashop.class);

        Unmarshaller unmarshaller = jc.createUnmarshaller();
        File xml = new File("src/test/resources/testData.xml");
        Prestashop prestashop = (Prestashop) unmarshaller.unmarshal(xml);

        Marshaller marshaller = jc.createMarshaller();
        marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
        marshaller.marshal(prestashop, System.out);
      }
    }

Solution 3:

EXAMPLE XML:

<a><bcategory='3'number='25'points='2'><c><dfilename=''content='some content'/><dfilename='0134.jpg'/></c><e><fdescription='desc'correct='1'/></e></a>

CODE FOR PARSING Fields in parsing class to make later modification easier:

//data to parseprivatestaticfinalStringTAG_A="a";        //rootprivatestaticfinalStringTAG_B="b";
privatestaticfinalStringATT_CATEGORY="category";
privatestaticfinalStringATT_POINTS="points";

privatestaticfinalStringTAG_D="d";
privatestaticfinalStringATT_FILE_NAME="filename";
privatestaticfinalStringATT_CONTENT="content";

privatestaticfinalStringTAG_F="f";
privatestaticfinalStringATT_DESCRIPTION="description";
privatestaticfinalStringATT_CORRECT="correct";

Method in parsing class:

privatevoidparseXml()
    {
        try
        {
            XmlPullParser parser = Xml.newPullParser();
            parser.setFeature(XmlPullParser.FEATURE_PROCESS_NAMESPACES, false);
            parser.setInput(questionsXmlFile, null);
            parser.nextTag();
            parser.require(XmlPullParser.START_TAG, null, TAG_A);
            while (parser.next() != XmlPullParser.END_DOCUMENT)
            {
                if (parser.getEventType() == XmlPullParser.START_TAG)
                {
                    String name = parser.getName();
                    if (name.equalsIgnoreCase(TAG_B))
                    {
                        parser.getAttributeValue(null, ATT_CATEGORY));
                        parser.getAttributeValue(null, ATT_POINTS));
                    }
                    elseif (name.equalsIgnoreCase(TAG_D))
                    {
                        parser.getAttributeValue(null, ATT_CONTENT));
                        parser.getAttributeValue(null, ATT_FILE_NAME));
                    }
                    elseif (name.equalsIgnoreCase(TAG_ANSWER))
                    {
                    }
                }
            }
        }
        catch (XmlPullParserException e)
        {
            e.printStackTrace();
        }
        catch (IOException e)
        {
            e.printStackTrace();
        }
    }

What this method does? It traverse through xml from beginning to END_DOCUMENT and check if tags has been found are interesting one. If yes, check desired attributes and do nothing with it (add your own actions. Remember, getAttributeValue() can return null. Be sure you dont operate on null. Remember, parser.require(XmlPullParser.START_TAG, null, TAG_A) returns exception if variables passed to it are wrong comparing current situation of parser.

Solution 4:

Your code looks all ok, only you have to remove <![CDATA[ .... ]]>

<id><![CDATA[ 526 ]]></id> change to <id>526</id>

or if you do not want to remove it, then do it following way,

Add the following method/function:

public String getCDataValue(Element e, String id) {
        NodeListtitle= e.getElementsByTagName(id);
        Elementex= (Element) title.item(0);
        Nodechild= ex.getFirstChild();
        if (child instanceof CharacterData) {
            CharacterDatacd= (CharacterData) child;
            return cd.getData().trim();
        }
        return"";
}

and then use it this way:

NodeListnl= doc.getElementsByTagName("product");
       for (inti=0; i < nl.getLength(); i++) {
        Elemente= (Element) nl.item(i);
        Toast.makeText(getApplicationContext(), getCDataValue(e, "price"),
        Toast.LENGTH_SHORT).show();
}

if its is your code :

publishProgress(new ProductItems(
                      getCDataValue(e, "name"),
                      getCDataValue(e, "id"),
                      getCDataValue(e, "id_default_image"),
                      12.050000)
);

Post a Comment for "Trouble With Xml Parsing Through Java"