简体   繁体   中英

How to return value from xpath in JAXB

I still new in JAXB, below are my codes listing.

My intention is to print this xpath //customers/customer/name by using following sample.

I have been trying to understand from @Blaise Doughan's blogs but it keep give me empty result. Anyone can pin point where is my mistakes and what should I do?

Sample 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. I hope this is useful!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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