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 stata安慰剂检验作图但是真实值不出现在图上
  • ¥15 c程序不知道为什么得不到结果
  • ¥40 复杂的限制性的商函数处理
  • ¥15 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题