Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>For this I would leverage an unmarshal event to store the info from the list of Mobile objects in a Map.</p> <p>Your domain model would look like:</p> <pre><code>import java.util.HashMap; import java.util.List; import java.util.Map; import javax.xml.bind.Unmarshaller; import javax.xml.bind.annotation.XmlAccessType; import javax.xml.bind.annotation.XmlAccessorType; import javax.xml.bind.annotation.XmlElement; import javax.xml.bind.annotation.XmlRootElement; @XmlRootElement(name="Details") @XmlAccessorType(XmlAccessType.NONE) public class Details { private Map&lt;String, String&gt; mobileNumberToUsername; @XmlElement(name="Mobile") private List&lt;Mobile&gt; mobileList; public Details() { mobileNumberToUsername = new HashMap&lt;String, String&gt;(); } public String getUsername(String mobileNumber) { return mobileNumberToUsername.get(mobileNumber); } void afterUnmarshal(Unmarshaller unmarshaller, Object parent) { for(Mobile mobile : mobileList) { mobileNumberToUsername.put(mobile.getMobileNumber(), mobile.getUsername()); } } } </code></pre> <p>and:</p> <pre><code>import javax.xml.bind.annotation.XmlElement; import javax.xml.bind.annotation.XmlType; @XmlType(propOrder={"username", "mobileNumber"}) public class Mobile { private String username; private String mobileNumber; @XmlElement(name="Username") public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } @XmlElement(name="MobileNumber") public String getMobileNumber() { return mobileNumber; } public void setMobileNumber(String mobileNumber) { this.mobileNumber = mobileNumber; } } </code></pre> <p>You can test out this mapping using your XML document and the following demo code:</p> <pre><code>import java.io.File; import javax.xml.bind.JAXBContext; import javax.xml.bind.Marshaller; import javax.xml.bind.Unmarshaller; public class Demo { public static void main(String[] args) throws Exception { JAXBContext jc = JAXBContext.newInstance(Details.class); Unmarshaller unmarshaller = jc.createUnmarshaller(); File xml = new File("src/forum121/input.xml"); Details details = (Details) unmarshaller.unmarshal(xml); System.out.println(details.getUsername("1234567890")); Marshaller marshaller = jc.createMarshaller(); marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true); marshaller.marshal(details, System.out); } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload