Java 从XML文件读取值时出错
我试图从XML文件中读取值,但当我试图将它们打印到控制台时,我得到了Java 从XML文件读取值时出错,java,xml,Java,Xml,我试图从XML文件中读取值,但当我试图将它们打印到控制台时,我得到了null值。我在这里给出了我的代码和XML文件 XML <?xml version="1.0" encoding="UTF-8" ?> <customer id="1" age="29" name="jeet"> </customer> 驱动程序 import java.io.File; import javax.xml.bind.JAXBContext; import jav
null
值。我在这里给出了我的代码和XML文件
XML
<?xml version="1.0" encoding="UTF-8" ?>
<customer id="1"
age="29"
name="jeet">
</customer>
驱动程序
import java.io.File;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Unmarshaller;
public class CheckClass {
public static void main(String[] args) {
try {
File file = new File("./file/NewFile.xml");
JAXBContext jaxbContext = JAXBContext.newInstance(Customer.class);
Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
Customer customer = (Customer) jaxbUnmarshaller.unmarshal(file);
System.out.println(customer.age);
System.out.println(customer.name);
} catch (JAXBException e) {
e.printStackTrace();
}
}
}
System.out.println(客户年龄)代码>
System.out.println(客户名称)代码>给我0
和null
值
有人能帮忙吗,谢谢。姓名和年龄不是要素。试试这个:
@XmlAttribute
public void setName(String name) {
this.name = name;
}
@XmlAttribute
public void setAge(int age) {
this.age = age;
}
导致您的问题的原因是您的java代码预期与该XML结构不匹配。您有两个选择:
将注释@xmlement
更改为@xmldattribute
或
将xml更改为:
<?xml version="1.0" encoding="UTF-8" ?>
<customer id="1">
<age>29</age>
<name>jeet</name>
</customer>
29
吉普车
为什么不使用
例如:
import com.thoughtworks.xstream.XStream;
import com.thoughtworks.xstream.annotations.XStreamAlias;
import com.thoughtworks.xstream.annotations.XStreamAsAttribute;
@XStreamAlias("Cat")
class Cat {
@XStreamAsAttribute
int age;
String name;
}
public class XStreamDemo {
public static void main(String[] args) {
XStream xstream = new XStream();
xstream.processAnnotations(Cat.class);
String xml = "<Cat age='4' ><name>Garfield</name></Cat>";
Cat cat = (Cat) xstream.fromXML(xml);
System.out.println("name -> " + cat.name);
System.out.println("age -> " + cat.age);
}
}
import com.thoughtworks.xstream.xstream;
导入com.thoughtworks.xstream.annotations.XStreamAlias;
导入com.thoughtworks.xstream.annotations.XStreamAsAttribute;
@XStreamAlias(“猫”)
班猫{
@XStreamAsAttribute
智力年龄;
字符串名;
}
公共类XStreamDemo{
公共静态void main(字符串[]args){
XStream XStream=新的XStream();
processAnnotations(Cat.class);
字符串xml=“Garfield”;
Cat=(Cat)xstream.fromXML(xml);
System.out.println(“名称->”+类别名称);
系统输出打印项次(“年龄->”+类别年龄);
}
}
您需要将Xstream jar文件添加到类路径中。我无法更改XML样式结构将是相同的。请使用第一个选项:更改注释这里不是问题
import com.thoughtworks.xstream.XStream;
import com.thoughtworks.xstream.annotations.XStreamAlias;
import com.thoughtworks.xstream.annotations.XStreamAsAttribute;
@XStreamAlias("Cat")
class Cat {
@XStreamAsAttribute
int age;
String name;
}
public class XStreamDemo {
public static void main(String[] args) {
XStream xstream = new XStream();
xstream.processAnnotations(Cat.class);
String xml = "<Cat age='4' ><name>Garfield</name></Cat>";
Cat cat = (Cat) xstream.fromXML(xml);
System.out.println("name -> " + cat.name);
System.out.println("age -> " + cat.age);
}
}