<?xml version="1.0" encoding="UTF-8"?>
<gamerForum>
<boardName>奇妙之旅 Online</boardName>
<newTopic>
<item>
<title><![CDATA[【問題】有沒有人知道這是個什麽樣的遊戲哦~]]></title>
<url><![CDATA[http://forum.gamer.com.tw/C.php?bsn=18416&snA=2]]></url>
<author><![CDATA[yankee5208]]></author>
<replyCount>2</replyCount>
<clickCount>152</clickCount>
<date>2010-05-27</date>
<time>19:20:02</time>
</item>
<item>
<title><![CDATA[【問題】這是款怎樣的遊戲呢]]></title>
<url><![CDATA[http://forum.gamer.com.tw/C.php?bsn=18416&snA=1]]></url>
<author><![CDATA[a5232232]]></author>
<replyCount>4</replyCount>
<clickCount>723</clickCount>
<date>2010-05-24</date>
<time>16:07:44</time>
</item>
</newTopic>
<hotTopic>
<item>
<title><![CDATA[【問題】有沒有人知道這是個什麽樣的遊戲哦~]]></title>
<url><![CDATA[http://forum.gamer.com.tw/C.php?bsn=18416&snA=2]]></url>
<author><![CDATA[yankee5208]]></author>
<replyCount>2</replyCount>
<clickCount>152</clickCount>
<date>2010-05-27</date>
<time>19:20:02</time>
</item>
</hotTopic>
</gamerForum>
數據時几口返回的,想知道在程序裏面怎樣读取用接口读取,并解析这些数据??

解决方案 »

  1.   

    很多工具都可以解析XML数据,常用的dom4j,jdom等。
    当然还可以借助OXM工具如castor、jaxb、xmlbeans等、、、
      

  2.   

    http://www.java3z.com/cwbwebhome/article/article2/2296.html?id=837
      

  3.   

    这种问题直接问Google,立马解决
      

  4.   

    很多,用dom或者sax解析;
     给你一个dom例子:
    User.xml<?xml version="1.0" encoding="UTF-8"?>
    <Users>
    <User id="1">
    <UserId type="Long">1</UserId>
    <UserName type="String">bobo</UserName>
    <TrueName type="String">谢XX</TrueName>
    <Password type="String">bbbb</Password>
    <Telephone type="String">XXXXXXXX</Telephone>
    </User>
    <Users>
    User.java:package com.tristan.dao.pojo;public class User {
    public Long getUserId() {
    return userId;
    } public void setUserId(Long userId) {
    this.userId = userId;
    } public String getUserName() {
    return userName;
    } public void setUserName(String userName) {
    this.userName = userName;
    } public String getTrueName() {
    return trueName;
    } public void setTrueName(String trueName) {
    this.trueName = trueName;
    } public String getPassword() {
    return password;
    } public void setPassword(String password) {
    this.password = password;
    } public String getTelephone() {
    return telephone;
    } public void setTelephone(String telephone) {
    this.telephone = telephone;
    } private Long userId;
    private String userName;
    private String trueName;
    private String password;
    private String telephone;}
    XmlParserHelper.java
    package com.tristan.dao.impl;import java.io.IOException;
    import java.io.InputStream;
    import java.lang.reflect.Field;
    import java.lang.reflect.InvocationTargetException;
    import java.lang.reflect.Method;
    import java.util.ArrayList;
    import java.util.List;import javax.xml.parsers.DocumentBuilder;
    import javax.xml.parsers.DocumentBuilderFactory;import org.w3c.dom.Document;
    import org.w3c.dom.Element;
    import org.w3c.dom.Node;
    import org.w3c.dom.NodeList;import com.tristan.dao.pojo.User;public class XmlParserHelper<T> { private String pojoFileName;
    private Class pojoClass;
    private String[] field; public Class getPojoClass() {
    return pojoClass;
    } // 设置pojo类名
    public void setPojoClass(Class pojoClass) {
    this.pojoClass = pojoClass;
    // pojo文件名
    this.pojoFileName = getPojoClass().getSimpleName() + ".xml"; Field[] tempField = getPojoClass().getDeclaredFields();
    field = new String[tempField.length];
    for (int i = 0; i < tempField.length; i++) {
    String temp = tempField[i].toString();
    int index = temp.lastIndexOf(".");
    field[i] = temp.substring(index);
    }
    } // 获取pojo的数据xml文件流
    public InputStream getInputStream() {
    return XmlParserHelper.class.getClassLoader().getResourceAsStream(
    pojoFileName);
    } public List<T> loadAll() throws Exception {
    List<T> list = new ArrayList<T>();
    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
    // 获取能解析xml文件为文档的的DocumentBuilder对象
    DocumentBuilder db = dbf.newDocumentBuilder();
    // 将指定的xml文件解析成文档对象
    Document doc = db.parse(getInputStream());
    // 获取文档的根节点元素
    Element element = doc.getDocumentElement();
    // 获取根节点元素集合
    NodeList nodelist = element.getChildNodes(); for (int i = 0; i < nodelist.getLength(); i++) {
    // 获取根节点下的子节点
    Node node = nodelist.item(i);
    if (node instanceof Element) {
    // 新建一pojo类
    T obj = (T) getPojoClass().newInstance();
    list.add(obj);
    // 获取子节点下的数据
    NodeList childNodeList = node.getChildNodes();
    for (int j = 0; j < childNodeList.getLength(); j++) {
    Node childNode = childNodeList.item(j); if (childNode instanceof Element) { Element e = (Element) childNode;
    if ("Long".equals(e.getAttribute("type"))) {
    Method method = getPojoClass()
    .getMethod("set" + childNode.getNodeName(),
    Long.class);
    method.invoke(obj, Long.parseLong(childNode
    .getTextContent()));
    } else if ("Integer".equals(e.getAttribute("type"))) {
    Method method = getPojoClass().getMethod(
    "set" + childNode.getNodeName(),
    Integer.class);
    method.invoke(obj, Integer.parseInt(childNode
    .getTextContent()));
    } else if ("Boolean".equals(e.getAttribute("type"))) {
    Method method = getPojoClass().getMethod(
    "set" + childNode.getNodeName(),
    Boolean.class);
    method.invoke(obj, Boolean.parseBoolean(childNode
    .getTextContent()));
    } else if ("Float".equals(e.getAttribute("type"))) {
    Method method = getPojoClass().getMethod(
    "set" + childNode.getNodeName(),
    Float.class);
    method.invoke(obj, Float.parseFloat(childNode
    .getTextContent()));
    } else if ("String".equals(e.getAttribute("type"))) {
    Method method = getPojoClass().getMethod(
    "set" + childNode.getNodeName(),
    String.class);
    method.invoke(obj, childNode.getTextContent());
    } else {
    throw new Exception("xml文件中数据类型不匹配");
    }
    } }
    } } return list;
    } // 加载id为id的pojo
    public T load(long id) throws Exception {
    T obj = null;
    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
    // 获取能解析xml文件为文档的的DocumentBuilder对象
    DocumentBuilder db = dbf.newDocumentBuilder();
    // 将指定的xml文件解析成文档对象
    Document doc = db.parse(getInputStream());
    // 获取文档的根节点元素
    Element element = doc.getDocumentElement();
    // 获取根节点元素集合
    NodeList nodelist = element.getChildNodes(); for (int i = 0; i < nodelist.getLength(); i++) {
    // 获取根节点下的子节点
    Node node = nodelist.item(i);
    if (node instanceof Element) {
    Element e1 = (Element) node;
    if (id == Long.parseLong(e1.getAttribute("id"))) {
    // 新建一pojo类
    obj = (T) getPojoClass().newInstance();
    // 获取子节点下的数据
    NodeList childNodeList = node.getChildNodes();
    for (int j = 0; j < childNodeList.getLength(); j++) {
    Node childNode = childNodeList.item(j); if (childNode instanceof Element) { Element e = (Element) childNode;
    if ("Long".equals(e.getAttribute("type"))) {
    Method method = getPojoClass().getMethod(
    "set" + childNode.getNodeName(),
    Long.class);
    method.invoke(obj, Long.parseLong(childNode
    .getTextContent()));
    } else if ("Integer".equals(e.getAttribute("type"))) {
    Method method = getPojoClass().getMethod(
    "set" + childNode.getNodeName(),
    Integer.class);
    method.invoke(obj, Integer.parseInt(childNode
    .getTextContent()));
    } else if ("Boolean".equals(e.getAttribute("type"))) {
    Method method = getPojoClass().getMethod(
    "set" + childNode.getNodeName(),
    Boolean.class);
    method.invoke(obj, Boolean
    .parseBoolean(childNode
    .getTextContent()));
    } else if ("Float".equals(e.getAttribute("type"))) {
    Method method = getPojoClass().getMethod(
    "set" + childNode.getNodeName(),
    Float.class);
    method.invoke(obj, Float.parseFloat(childNode
    .getTextContent()));
    } else if ("String".equals(e.getAttribute("type"))) {
    Method method = getPojoClass().getMethod(
    "set" + childNode.getNodeName(),
    String.class);
    method.invoke(obj, childNode.getTextContent());
    } else {
    throw new Exception("xml文件中数据类型不匹配");
    }
    } } } }
    }
    return obj;
    } // 更新pojo
    public void update(T pojo) {

    } // 删除pojo
    public void remove(T pojo) {

    } public static void main(String[] args) {
    XmlParserHelper<User> helper = new XmlParserHelper<User>();
    helper.setPojoClass(User.class);
    try {
    List<User> list = helper.loadAll();
    for (int i = 0; i < list.size(); i++) {
    System.out.println(list.get(i).getUserId());
    }
    User user = helper.load(1);
    System.out.println(user.getPassword()); } catch (Exception e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    } }
    }
      

  5.   

    你这个是rss吧
    解析其实很简单的
    网上应该都能搜到rss解析的代码