如何从JAXB中的xpath返回值



我还是JAXB的新手,下面是我的代码列表。

我的意图是通过使用以下示例打印此xpath//customers/customer/name

我一直试图从@Blaise Doughan的博客中理解,但它总是给我一个空洞的结果。任何人都可以指出我的错误在哪里,我该怎么办?

示例XML

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<customers>
    <customer id="100">
        <name>customer1</name>
        <age>29</age>
    </customer>
    <customer id="200">
        <age>39</age>
        <name>customer2</name>
    </customer>
</customers>

我有主类

package performancetest.JAXB_Unmarshalling;
import java.io.File;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.Unmarshaller;
public class JAXBExample {
    public static void main(String[] args) {
            getByCustomerName();
    }
        public static void getByCustomerName() {
            try {
                JAXBContext jc = JAXBContext.newInstance(CustomerNamesList.class);
                Unmarshaller unmarshaller = jc.createUnmarshaller();
                CustomerNamesList obj = (CustomerNamesList)unmarshaller.unmarshal(new File("C:\Users\iman.solaiman\Documents\file.xml"));
                System.out.println(obj.getCustomers());
            }catch (Exception e) {
                e.printStackTrace();
            }
        }
}

客户名单

package performancetest.JAXB_Unmarshalling;
import java.util.ArrayList;
import java.util.List;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
@XmlAccessorType(XmlAccessType.FIELD)
@XmlRootElement(name = "customers")
public class CustomerNamesList {
    @XmlElement(name="customer/name")
    private List<CustomerNames> customerNamesList = new ArrayList<CustomerNames>();
    public List<CustomerNames> getCustomers() {
        return customerNamesList;
    }
    public void Customers(List<CustomerNames> CustomerNames) {
        this.customerNamesList = CustomerNames;
    }
    public void getElement () {
        for (int i=0; i<customerNamesList.size(); i++){
            System.out.println("Element "+i+": "+customerNamesList.get(i));
        }
    }
}

客户名称

package performancetest.JAXB_Unmarshalling;
import org.eclipse.persistence.oxm.annotations.XmlPath;
public class CustomerNames {
    @XmlPath("customers/customer/name/text()")
    String name;
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String toString() {
        return "Customer [name=" + name+ "]";
    }
}

结果:

run:
[]
BUILD SUCCESSFUL (total time: 1 second)

我发现使用以下方法更容易做到这一点:

private String getXPath(String xml) throws XPathExpressionException {
    String xPathResult= "";
    InputSource source = new InputSource(new StringReader(xml));
    XPath xpath = XPathFactory.newInstance().newXPath();
    Object jaxBObject = xpath.evaluate("/customers", source, XPathConstants.NODE);
    xPathResult= xpath.evaluate("name", jaxBObject);
    return xPathResult;
}

这将为您提供name元素内部的值。我希望这是有用的!

最新更新