parsing xml file using java for — android based application

夙愿已清 提交于 2019-12-10 17:32:59

问题


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?


回答1:


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




回答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/



来源:https://stackoverflow.com/questions/12855391/parsing-xml-file-using-java-for-android-based-application

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!