简体   繁体   English

如何从JAXB中的xpath返回值

[英]How to return value from xpath in JAXB

I still new in JAXB, below are my codes listing. 我还是JAXB的新手,下面是我的代码清单。

My intention is to print this xpath //customers/customer/name by using following sample. 我的意图是通过使用以下示例来打印此xpath // customers / customer / name

I have been trying to understand from @Blaise Doughan's blogs but it keep give me empty result. 我一直试图从@Blaise Doughan的博客中了解,但它总是给我空洞的结果。 Anyone can pin point where is my mistakes and what should I do? 任何人都可以指出我的错误在哪里,我该怎么办?

Sample XML 样本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>

I have Main Class 我有主班

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();
            }
        }
}

And Customer Name List 客户名称列表

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));
        }
    }
}

And Customer Names 客户名称

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+ "]";
    }
}

Result: 结果:

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

I find that this is easier to do with the method below: 我发现使用以下方法更容易做到这一点:

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;
}

This should give you the value inside the name element. 这应该为您提供name元素内的值。 I hope this is useful! 我希望这是有用的!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM