解析XML文件中使用Java进行 - 基于Android的应用程序 [英] parsing xml file using java for -- android based application

查看:162
本文介绍了解析XML文件中使用Java进行 - 基于Android的应用程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是新来解析XML文件中的Java。我如何从属性和值分析值所在的标签,但在我的XML中的值位于不同位置的一些想法:

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>

这是我的XML格式的,在这里我想从XML解析的百分比值。如果有人知道如何解析值,恳求引导我?

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?

推荐答案

有几种可能做到这一点。

There are several possibilities to do this.

您可以使用例如simplexml的框架。

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类:

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() {
   }

}

李类别:

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

做任何你想要的ulTag。例如:

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

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

可能这可以帮助你。填写给我写。 这是一个关于SimpleXML的框架链接 http://simple.sourceforge.net/download/stream/doc/tutorial /tutorial.php

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

这篇关于解析XML文件中使用Java进行 - 基于Android的应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆