आप एक XMLStreamReader
का लाभ उठाकर JAXB में ऐसा कर सकता है और एक Unmarshaller.Listener
:
डेमो
package forum383861;
import java.io.FileInputStream;
import java.util.HashMap;
import java.util.Map;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.Unmarshaller;
import javax.xml.bind.Unmarshaller.Listener;
import javax.xml.stream.Location;
import javax.xml.stream.XMLInputFactory;
import javax.xml.stream.XMLStreamReader;
public class Demo {
public static void main(String[] args) throws Exception {
JAXBContext jc = JAXBContext.newInstance(Customer.class);
XMLInputFactory xif = XMLInputFactory.newFactory();
FileInputStream xml = new FileInputStream("src/forum383861/input.xml");
XMLStreamReader xsr = xif.createXMLStreamReader(xml);
Unmarshaller unmarshaller = jc.createUnmarshaller();
LocationListener ll = new LocationListener(xsr);
unmarshaller.setListener(ll);
Customer customer = (Customer) unmarshaller.unmarshal(xsr);
System.out.println(ll.getLocation(customer));
System.out.println(ll.getLocation(customer.getAddress()));
}
private static class LocationListener extends Listener {
private XMLStreamReader xsr;
private Map<Object, Location> locations;
public LocationListener(XMLStreamReader xsr) {
this.xsr = xsr;
this.locations = new HashMap<Object, Location>();
}
@Override
public void beforeUnmarshal(Object target, Object parent) {
locations.put(target, xsr.getLocation());
}
public Location getLocation(Object o) {
return locations.get(o);
}
}
}
input.xml
<?xml version="1.0" encoding="UTF-8"?>
<customer>
<address/>
</customer>
आउटपुट
[row,col {unknown-source}]: [2,1]
[row,col {unknown-source}]: [3,5]
ग्राहक
package forum383861;
import javax.xml.bind.annotation.XmlRootElement;
@XmlRootElement
public class Customer {
private Address address;
public Address getAddress() {
return address;
}
public void setAddress(Address address) {
this.address = address;
}
}
पता
package forum383861;
public class Address {
}
अधिक जानकारी के लिए
स्रोत
2011-08-16 16:15:57
पूरी तरह से काम किया, बहुत बहुत धन्यवाद। मेरे लिए एकमात्र छोटी अप्रत्याशित बात यह है कि यह उद्घाटन टैग की शुरुआत के बजाय एक्सएमएल तत्व के उद्घाटन टैग के अंत का स्थान देता है। लेकिन यह मेरे आवेदन के लिए काफी अच्छा है। – lexicalscope