我試圖從使用JAXB的xml檢索值。下面詳細:JAXB從xml文件檢索值的程序
我OrderValidation.xml
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<OrderValidation>
<OrderType name="Activation-Activation">
<Product>
<Name>TV_SAT</Name>
<ActionCode>ADD</ActionCode>
</Product>
</OrderType>
<OrderType name="Change Owner-Change Owner">
<Product>
<Name>TV_SAT_EQUIPMENT</Name>
<ActionCode>EXISTING</ActionCode>
</Product>
</OrderType>
</OrderValidation>
這裏是我的Orderxml.java
@XmlRootElement(name ="OrderValidation")
public class Orderxml
{
private String name;
private String Product;
private String OrderType;
public Orderxml() {}
public Orderxml(String name, String productclass, String ordertype)
{
super();
this.name = name;
this.Product = productclass;
this.OrderType = ordertype;
}
@XmlAttribute
public String getName()
{
return name;
}
public void setName(String name)
{
this.name = name;
}
@XmlElement
public String getProductName()
{
return Product;
}
public void setProductName(String productclass)
{
this.Product = productclass;
}
@XmlElement
public String getOrderType()
{
return OrderType;
}
public void setOrderType(String ordertype)
{
this.OrderType = ordertype;
}
}
我的解組代碼(主類)
public static void main(String[] args)
{
try
{
File file = new File("C:///OrderValidation.xml");
System.out.println(1);
JAXBContext jaxbContext = JAXBContext.newInstance(Orderxml.class);
System.out.println(2);
Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
System.out.println(3);
Orderxml ord= (Orderxml) jaxbUnmarshaller.unmarshal(file);
System.out.println(4);
System.out.println(ord.getOrderType()+". "+ord.getProductName());
}
catch (JAXBException e)
{
System.out.println("Issue is here");
e.printStackTrace();
}
}
當我執行它的代碼是拋出null作爲輸出。請向我建議代碼中的更改以檢索值。在此先感謝
您需要提供異常堆棧跟蹤。 – OTM
沒有例外。輸出即將爲空。 這是我的輸出 null。 null – kanni
您的類定義'Orderxml'與您提供的XML不匹配......這就是爲什麼您需要'null' –