2011-08-16 5 views
5

मेरे पास JAXB द्वारा XML फ़ाइल से कुछ ऑब्जेक्ट्स को अनारक्षित किया गया है। क्या जेएक्सबी मुझे बता सकता है या किसी भी तरह से यह पता लगाना संभव है कि एक्सएमएल फ़ाइल (लाइन और कॉलम) में प्रत्येक ऑब्जेक्ट कहां से आता है?Unmarshalled ऑब्जेक्ट्स के लिए फ़ाइल में JAXB स्थान

यह जानकारी किसी बिंदु पर उपलब्ध है, क्योंकि जेएक्सबी स्कीमा सत्यापन त्रुटियों के दौरान मुझे यह देता है। लेकिन मैं इसे मान्य वस्तुओं के लिए भी उपलब्ध करना चाहता हूं।

उत्तर

9

आप एक 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 { 

} 

अधिक जानकारी के लिए

+0

पूरी तरह से काम किया, बहुत बहुत धन्यवाद। मेरे लिए एकमात्र छोटी अप्रत्याशित बात यह है कि यह उद्घाटन टैग की शुरुआत के बजाय एक्सएमएल तत्व के उद्घाटन टैग के अंत का स्थान देता है। लेकिन यह मेरे आवेदन के लिए काफी अच्छा है। – lexicalscope

2

मुझे डर नहीं है। जेएक्सबी एक एक्सएमएल पार्सर के शीर्ष पर बनाता है, यह आपके दस्तावेज़ के मूल स्ट्रिंग प्रस्तुति को भूलने वाले आपके XML दस्तावेज़ का तार्किक प्रतिनिधित्व करेगा।

आपकी स्ट्रिंग अभी भी पढ़ने के दौरान सत्यापन चरण किया जाता है, इसलिए आपका पार्सर आपको त्रुटि की स्थिति बताते हुए एक त्रुटि संदेश देने में सक्षम है। जेएक्सबी केवल उस त्रुटि संदेश को बाईपास करेगा। लेकिन जैसे ही एक्सएमएल मान्य और पार्स किया जाता है, केवल तार्किक प्रतिनिधित्व मौजूद होगा।

+0

आप एक 'XMLStreamReader' और एक' Unmarshaller.Listener' का लाभ उठाने के समर्थन के इस प्रकार प्राप्त करने के लिए कर सकते हैं: http://stackoverflow.com/questions/7079796/jaxb-location-in-file-for -unmarshalled-items/7081390 # 7081390 –

संबंधित मुद्दे