asdfassdfa在江湖 2016-04-05 11:20 采纳率: 0%
浏览 1889

java中解析xml,要求在控制台输出但是输出的是null

package pero.common.Metied;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.text.MessageFormat;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Unmarshaller;

public class JaxbRootXml {
@SuppressWarnings("unchecked")
public static T readString(Class clazz,String context)throws JAXBException{
try{
JAXBContext jc=JAXBContext.newInstance(clazz);
Unmarshaller u=jc.createUnmarshaller();
return (T) u.unmarshal(new File(context));
}catch(JAXBException e){
throw e;
}
}

@SuppressWarnings("unchecked")
public static <T> T readConfig(Class<T> clazz, String config, Object... arguments) throws IOException,
        JAXBException {
    InputStream is = null;
    try {
        if (arguments.length > 0) {
            config = MessageFormat.format(config, arguments);
        }
        JAXBContext jc = JAXBContext.newInstance(clazz);
        Unmarshaller u = jc.createUnmarshaller();
        is = new FileInputStream(config);
        return (T) u.unmarshal(is);
    } catch (IOException e) {
        throw e;
    } catch (JAXBException e) {
        throw e;
    } finally {
        if (is != null) {
            is.close();
        }
    }
}


@SuppressWarnings("unchecked")
public static <T> T readConfigFromStream(Class<T> clazz, InputStream dataStream) throws JAXBException {
    try {
        JAXBContext jc = JAXBContext.newInstance(clazz);
        Unmarshaller u = jc.createUnmarshaller();
        return (T) u.unmarshal(dataStream);
    } catch (JAXBException e) {
        throw e;
    }
}


public static void main(String[] args) throws JAXBException {
    TestRoot tr=JaxbRootXml.readString(TestRoot.class, "test/1.xml");

    for(TestUser u:tr){
        System.out.println(u.getId());
            System.out.println(u.getName());
            System.out.println(u.getPassword());
            System.out.println(u.getAge());
    }
}

}

package pero.common.Metied;

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

import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;

@SuppressWarnings("serial")
@XmlRootElement(name="Root")
public class TestRoot extends ArrayList{

@XmlElement(name = "user")
public List<TestUser> getUsers(){
    return this;
}

}

package pero.common.Metied;

import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlAttribute;

@XmlAccessorType(XmlAccessType.FIELD)
public class TestUser {

@XmlAttribute(name = "id")
private String id;

public String getId() {
    return id;
}
public void setId(String id) {
    this.id = id;
}

@XmlAttribute(name = "name")
private String name;
@XmlAttribute(name = "password")
private String password;
@XmlAttribute(name = "age")
private String age;
public String getName() {
    return name;
}
public void setName(String name) {
    this.name = name;
}
public String getPassword() {
    return password;
}
public void setPassword(String password) {
    this.password = password;
}
public String getAge() {
    return age;
}
public void setAge(String age) {
    this.age = age;
}

}

此处是要解析的

  • user112320
  • user245621
  • user312321
  • user412321
  • 写回答

3条回答 默认 最新

  • 友之游 2016-04-06 00:31
    关注

    用debug或者log找出哪一行报异常再问估计会有人帮你解决。

    评论

报告相同问题?

悬赏问题

  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog