I am new to Java programming and I am doing Unmarshalling the following XML string. My task is get the names of the customers in this string. I have done it for one customer. I
If you don't want to map a class to the outer most data element, below is how you could use StAX as per your original question.
import java.io.*;
import javax.xml.bind.*;
import javax.xml.stream.*;
public class Demo {
public static void main(String[] args) throws Exception {
XMLInputFactory xif = XMLInputFactory.newFactory();
FileReader reader = new FileReader("input.xml");
XMLStreamReader xsr = xif.createXMLStreamReader(reader);
JAXBContext jc = JAXBContext.newInstance(Customer.class);
Unmarshaller unmarshaller = jc.createUnmarshaller();
while(xsr.hasNext()) {
while(xsr.hasNext() && (!xsr.isStartElement() || !xsr.getLocalName().equals("customer"))) {
xsr.next();
}
if(xsr.hasNext()) {
Customer customer = (Customer) unmarshaller.unmarshal(xsr);
System.out.println(customer);
}
}
}
}