Parsing an XML file using java for-android application

I am new to parsing an XML file in java. I have some idea on how to parse values ​​from attributes and values ​​from tags, but in my XML the values ​​are in another place:

<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 analyze the percentage values ​​from XML. If someone knows how to disassemble values, please guide me?

+4
source share
2 answers

There are several ways to do this.

You can use, for example, simplexml Framework.

To do this, you can try to create several classes that will help you solve the problem.

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 into it. but here we do not need him.

 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; } } 

Removing a deserialization of a simple object

 Serializer serializer = new Persister(); File source = new File("yourxmlexampl.xml"); ULTag ulTag = serializer.read(ULTag.class, source); 

Do whatever you want with ulTag. eg:

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

Maybe this can help you. Fill me to write. This is a link about the simplexml structure http://simple.sourceforge.net/download/stream/doc/tutorial/tutorial.php

+3
source

For XML parsing, you have several options in Android, DOM parser, Pull parser, etc. Faster and more recommended is XMLPullParser (see http://developer.android.com/training/basics/network-ops/xml.html ).

I use it and it works much better than the DOM parser for big data.

If you don't have big data for parsing, you can use the DOM parser.

Here is the documentation or 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 on parsing XML in Android

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

+2
source

Source: https://habr.com/ru/post/1439333/


All Articles