4

I am new to parsing xml file in java. I have some idea of how to parse values from attributes and values resides from tags but in my XML the values resides in different location:

<ul xml:base="http//www.example.com">
    <li>
        <strong>Ram</strong>
        : 45%
    </li>
    <li>
        <strong>CPU</strong>
        : 49%
    </li>
    <li>
        <strong>Undecided</strong>
        : 6%
    </li>
</ul>

This is my XML format, here I want to parse the percentage values from the XML. If anyone knows how to parse the values, pleas guide me?

2 Answers 2

3

There are several possibilities to do this.

You can use for example the simplexml Framework.

For that you can try to create some classes which can help you to solve the issue.

UL class:

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

import org.simpleframework.xml.ElementList;
import org.simpleframework.xml.Root;

@Root(name = "ul", strict = false)
public class ULTag {

   @ElementList(name = "li", inline = true, required = false)
   List<LITag> liTags = new ArrayList<LITag>();

   public List<LITag> getLiTags() {
      return liTags;
   }

   public void setLiTags(List<LITag> liTags) {
      this.liTags = liTags;
   }

   public ULTag() {
   }

}

Li Class:

import org.simpleframework.xml.Element;
import org.simpleframework.xml.Root;

@Root(name = "li", strict = false)
public class LITag {

   @Element(name = "strong", required = false)
   private String strong;

   public LITag() {
   }

   public String getStrong() {
      return strong;
   }

   public void setStrong(String strong) {
      this.strong = strong;
   }

}

you can also create a strong class if you want to insert more things in it. but here we don't need it.

import org.simpleframework.xml.Element;
import org.simpleframework.xml.Root;

@Root(name = "strong", strict = false)
public class StrongTag {

   @Element(name = "strong", required = false)
   private String strong;

   public StrongTag() {
   }

   public String getStrong() {
      return strong;
   }

   public void setStrong(String strong) {
      this.strong = strong;
   }
}

Deserializing a simple object

Serializer serializer = new Persister();
File source = new File("yourxmlexampl.xml");

ULTag ulTag = serializer.read(ULTag.class, source);

Do what ever you want with ulTag. e.g.:

String percent=ulTag.getLITags().get(0).getStrong();

May be this can help you. Fill free to write me. This is a link about simplexml framework http://simple.sourceforge.net/download/stream/doc/tutorial/tutorial.php

Sign up to request clarification or add additional context in comments.

Comments

2

For XML parsing, you have several choices in Android, DOM parser, Pull parser etc. The faster and recommanded one is XMLPullParser (see http://developer.android.com/training/basics/network-ops/xml.html).

I am using it and it works much better than the DOM parser for large data.

If you don't have large data to parse, you can use the DOM parser.

Here is the documentation or the XMLPullParser: http://developer.android.com/reference/org/xmlpull/v1/XmlPullParser.html

And a tutorial on how to use it: http://www.vogella.com/articles/AndroidXML/article.html

And here is a general tutorial about XML parsing in Android

http://www.ibm.com/developerworks/opensource/library/x-android/

4 Comments

thanks Milos,i have some idea of jdom. but my xml contains tags like <li> <strong>Ram</strong> : 45% </li>. i can know how to parse values from attributes and direct tags. but i'm new to this kind of tags. can you pls guide me on how to parse values from <strong> and percentage values. Thanks in advance.
To get text in jdom, you can use Element.getText() method.
As @user802421 says, you can use the method getText(). It's the same for the XMLPullParser. Once you get your element, you can extract only his value (inside of the tag) with getText();
If you are using dom, Simple XML could be interesting. simple.sourceforge.net

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.