我无法解组代表此格式的发票的XML:
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<soap:Body>
<Response xmlns="Lamp">
<Result>
<Title>The Title</Title>
<Lines>
<anyType xsi:type="InvoiceLine">
<Name>Name One</Name>
<Quantity>1.0000</Quantity>
</anyType>
<anyType xsi:type="InvoiceLine">
<Name>Name Two</Name>
<Quantity>2.0000</Quantity>
</anyType>
</Lines>
</Result>
</Response>
</soap:Body>
</soap:Envelope>
发票类:
@XmlAccessorType(XmlAccessType.FIELD)
public class Invoice {
public String Title;
public List<InvoiceLine> Lines;
}
InvoiceLine类:
@XmlAccessorType(XmlAccessType.FIELD)
public class InvoiceLine extends anyType {
public String Name;
public float quantity;
}
anyType的抽象类:
public abstract class anyType {}
这是我用来进行解组的代码:
public static void main(String[] args) throws Exception {
InputStream is = new FileInputStream("input.xml");
XMLInputFactory xif = XMLInputFactory.newFactory();
XMLStreamReader xsr = xif.createXMLStreamReader(is);
xsr.nextTag();
while(!xsr.getLocalName().equals("Result")) {
xsr.nextTag();
}
JAXBContext jc = JAXBContext.newInstance(Invoice.class, InvoiceLine.class);
javax.xml.bind.Unmarshaller unmarshaller = jc.createUnmarshaller();
JAXBElement<Invoice> jb = unmarshaller.unmarshal(xsr, Invoice.class);
xsr.close();
System.out.println(jb.getValue());
}
问题是,Lines列表只包含1个Name = null和quantity = 0.0的条目。
- 编辑:
我刚尝试将XmlElement和XmlElementWrapper注释添加到这两个类中,唯一的变化是Lines列表没有元素。
修改发票:
@XmlAccessorType(XmlAccessType.FIELD)
public class Invoice {
@XmlElement(name = "Title")
public String Title;
@XmlElement(name = "anyType")
@XmlElementWrapper(name = "Lines")
public List<InvoiceLine> Lines;
}
修改后的InvoiceLine:
@XmlAccessorType(XmlAccessType.FIELD)
public class InvoiceLine extends anyType {
@XmlElement(name = "Name")
public String Name;
@XmlElement(name = "Quantity")
public float Quantity;
}
- 编辑: 我只是尝试将属性设置为小写,但仍然没有运气。