XML结构:
<dataroot>
<CustomerData>
<DOCDELIVERY>
<LocationType>xyz</LocationType>
<DocumentName>docuName</DocumentName>
<OutputFilePath>some path</OutputFilePath>
<OutputFileName>file name</OutputFileName>
</DOCDELIVERY>
<LABEL>
<ItemNumber>007</ItemNumber>
<VendorHeader1>vendor header1</VendorHeader1>
<data1></data1>
<data2></data2>
</LABEL>
</CustomerData>
</dataroot>
下面是我正在执行XML解组的类:
public class ReadXml {
public void readXml(String inputXml)
throws ParserConfigurationException, SAXException, IOException,
JAXBException {
File inputXmlFile = new File(inputXml);
JAXBContext jaxbContext = JAXBContext.newInstance(Dataroot.class);
Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
Dataroot dataRoot = (Dataroot) unmarshaller.unmarshal(inputXmlFile);
CustomerData customerData = dataRoot.getCustomerData();
DocDelivery docDelivery = customerData.getDOCDELIVERY();
Label label = customerData.getLABEL();
System.out.println(label.getVendorHeader1());
//return label;
//System.out.println(label.getVendorHeader1());
}
}
以下是XML结构的POJO类:
CustomerData.java
@XmlAccessorType(XmlAccessType.FIELD)
public class CustomerData {
@XmlElement(name = "DOCDELIVERY")
private DocDelivery DOCDELIVERY;
@XmlElement(name = "LABEL")
private Label LABEL;
public CustomerData() {
};
public DocDelivery getDOCDELIVERY() {
return DOCDELIVERY;
}
public void setDOCDELIVERY(DocDelivery docDelivery) {
DOCDELIVERY = docDelivery;
}
public Label getLABEL() {
return LABEL;
}
public void setLABEL(Label label) {
this.LABEL = label;
}
}
Dataroot.java
@XmlRootElement(name = "dataroot")
@XmlAccessorType(XmlAccessType.FIELD)
public class Dataroot {
@XmlElement(name = "CustomerData")
private CustomerData CustomerData;
public Dataroot() {
};
public CustomerData getCustomerData() {
return CustomerData;
}
public void setCustomerData(CustomerData customerData) {
this.CustomerData = customerData;
}
}
Label.java
@XmlAccessorType(XmlAccessType.FIELD)
public class Label {
@XmlElement(name = "VendorHeader1")
private String VendorHeader1;
public Label() {
};
public String getVendorHeader1() {
return VendorHeader1;
}
public void setVendorHeader1(String VendorHeader1) {
this.VendorHeader1 = VendorHeader1;
}
}
我想在另一个类的ReadXML类中调用readXML方法,我想访问下面类中的Label标记值。 具体来说,我想在下面的类&amp;中的inputXML方法中调用readXML方法。访问Label类的getter / setter方法。 如何实现这一目标?
FXMLController.java
public class FXMLController {
@FXML
private Button inputXmlFileBtn;
@FXML
private TextField inputXmlName;
public void inputXmlFileChooser() throws ParserConfigurationException, SAXException, IOException, JAXBException {
FileChooser fileChooser = new FileChooser();
// Set extension filter
fileChooser.getExtensionFilters().addAll(
new ExtensionFilter("XML files (*.xml)", "*.xml"));
// Open Dialog
File file = fileChooser.showOpenDialog(null);
// Set the path for inputXmlName text field
if (file != null) {
inputXmlName.setText(file.getPath());
}
}
}