2013-01-22 58 views

回答

3

EclipseLink JAXB (MOXy)和JAXB參考實現各自具有它們自己的註釋@XmlLocation用於支持這種使用情況。這允許您將該對象所對應的XML元素上的位置作爲org.xml.sax.Locator的實例存儲。由於我的莫西領先,我將演示如何使用莫西:

import javax.xml.bind.annotation.*; 
import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter; 
import org.eclipse.persistence.oxm.annotations.XmlLocation; 
import org.xml.sax.Locator; 

@XmlRootElement 
@XmlAccessorType(XmlAccessType.FIELD) 
public class Person { 

    @XmlJavaTypeAdapter(value=StringAdapter.class) 
    String name; 

    Address address; 

    @XmlLocation 
    Locator locator; 

} 

地址

import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter; 
import org.eclipse.persistence.oxm.annotations.XmlLocation; 
import org.xml.sax.Locator; 

public class Address { 

    @XmlJavaTypeAdapter(value=StringAdapter.class) 
    private String street; 

    @XmlLocation 
    Locator locator; 

} 

StringAdapter

import javax.xml.bind.annotation.*; 
import javax.xml.bind.annotation.adapters.XmlAdapter; 
import org.eclipse.persistence.oxm.annotations.XmlLocation; 
import org.xml.sax.Locator; 

public class StringAdapter extends XmlAdapter<StringAdapter.AdaptedString, String> { 

    public static class AdaptedString { 

     @XmlValue 
     public String value; 

     @XmlLocation 
     @XmlTransient 
     Locator locator; 

    } 

    @Override 
    public String unmarshal(AdaptedString v) throws Exception { 
     System.out.println(v.value + " " + v.locator.getLineNumber()); 
     return v.value; 
    } 

    @Override 
    public AdaptedString marshal(String v) throws Exception { 
     AdaptedString adaptedString = new AdaptedString(); 
     adaptedString.value = v; 
     return adaptedString; 
    } 

} 
你需要個

jaxb.properties

要指定莫西爲您的JAXB提供者包括在同一個包中的以下項域模型稱爲jaxb.properties文件。

javax.xml.bind.context.factory=org.eclipse.persistence.jaxb.JAXBContextFactory 

演示

import java.io.File; 
import javax.xml.bind.*; 

public class Demo { 

    public static void main(String[] args) throws Exception { 
     JAXBContext jc = JAXBContext.newInstance(Person.class); 

     Unmarshaller unmarshaller = jc.createUnmarshaller(); 
     File xml = new File("src/forum14455596/input.xml"); 
     Person person = (Person) unmarshaller.unmarshal(xml); 

     System.out.println("Person: " + person.locator.getLineNumber()); 
     System.out.println("Address: " + person.address.locator.getLineNumber()); 
    } 

} 

輸出

Jane Doe 3 
1 A Street 5 
Person: 2 
Address: 4 
+0

在上面的例子中,是否有可能獲得XML文件中字符串值的Street的行號 –

+0

@AnandB - 您想在哪裏存儲「Street」的行號?同樣出於好奇,你爲什麼要存儲所有的行號? –

+0

@AnandB - 我已經用可能有效的'XmlAdapter'方法更新了我的答案。最終你可能不想使用'String'屬性,直接在你的模型中使用像'AdaptedString'這樣的類。 –

2

你可以充分利用的StAX StreamReaderDelegate並做類似如下:

演示

import javax.xml.bind.*; 
import javax.xml.stream.*; 
import javax.xml.stream.util.StreamReaderDelegate; 
import javax.xml.transform.stream.StreamSource; 

public class Demo { 

    public static void main(String[] args) throws Exception { 

     JAXBContext jc = JAXBContext.newInstance(Person.class); 

     XMLInputFactory xif = XMLInputFactory.newFactory(); 
     StreamSource source = new StreamSource("src/forum14455596/input.xml"); 
     XMLStreamReader xsr = xif.createXMLStreamReader(source); 
     xsr = new StreamReaderDelegate(xsr) { 

      @Override 
      public String getLocalName() { 
       String localName = super.getLocalName(); 
       if(isStartElement()) { 
        System.out.println(localName + " " + this.getLocation().getLineNumber()); 
       } 
       return localName; 
      } 

     }; 

     Unmarshaller unmarshaller = jc.createUnmarshaller(); 
     unmarshaller.unmarshal(xsr); 
    } 

} 

import javax.xml.bind.annotation.*; 

@XmlRootElement 
@XmlAccessorType(XmlAccessType.FIELD) 
public class Person { 

    private String name; 
    private String address; 

} 

的input.xml

<?xml version="1.0" encoding="UTF-8"?> 
<person> 
    <name>Jane Doe</name> 
    <address>1 A Street</address> 
</person> 

輸出

person 2 
name 3 
address 4 
+0

我如何能夠達到同樣的嵌套XML文件的情況。是否有可能在個人課堂內擁有像Locator這樣的財產。 –

+0

@AnandB - 無論XML的深度如何,該代碼都會輸出每個啓動元素的行號。 –

+0

在單獨的課程中是否可以擁有像Locator這樣的屬性? –