1
我有一個JAXB類JAXB /莫西:@XmlLocation定位器不起作用
@XmlTransient
@XmlLocation Locator location;
public Locator getLocation() { return location; }
但解組(從XML)之後,該值爲null。 使用MOXy 2.5.0,JDK 1.7.21。
什麼可能是錯誤的?
我有一個JAXB類JAXB /莫西:@XmlLocation定位器不起作用
@XmlTransient
@XmlLocation Locator location;
public Locator getLocation() { return location; }
但解組(從XML)之後,該值爲null。 使用MOXy 2.5.0,JDK 1.7.21。
什麼可能是錯誤的?
EclipseLink JAXB (MOXy)的@XmlLocation
確實有效,但是如果您從DOM Node
解組,那麼它將不會捕獲任何位置信息。我將在下面舉例說明。
Java模型
下面是我們將使用這個例子的Java模型。
富
import javax.xml.bind.annotation.*;
@XmlRootElement
@XmlAccessorType(XmlAccessType.FIELD)
public class Foo {
Bar bar;
}
酒吧
我們將使用在Bar
類@XmlLocation
註釋存儲的位置。 MOXy支持來自JAXB參考實現的@XmlLocation
批註(包括在內部軟件包中重新打包的那個)以及它自己的版本。
// import com.sun.xml.bind.annotation.XmlLocation;
// import com.sun.xml.internal.bind.annotation.XmlLocation;
import javax.xml.bind.annotation.*;
import org.eclipse.persistence.oxm.annotations.XmlLocation;
import org.xml.sax.Locator;
@XmlAccessorType(XmlAccessType.FIELD)
public class Bar {
@XmlTransient
@XmlLocation
Locator location;
}
XML INPUT(input.xml中)
<?xml version="1.0" encoding="UTF-8"?>
<foo>
<bar/>
</foo>
DEMO CODE
下面是一些演示代碼,將解組不同類型的輸入,並且然後輸出的位置。
import java.io.File;
import javax.xml.bind.*;
import javax.xml.parsers.*;
import javax.xml.stream.*;
import javax.xml.transform.Source;
import javax.xml.transform.stream.StreamSource;
import org.w3c.dom.Document;
import org.xml.sax.*;
public class Demo {
public static void main(String[] args) throws Exception {
JAXBContext jc = JAXBContext.newInstance(Foo.class);
Unmarshaller unmarshaller = jc.createUnmarshaller();
File file = new File("src/forum17288002/input.xml");
Foo foo1 = (Foo) unmarshaller.unmarshal(file);
outputLocation(file, foo1);
InputSource inputSource = new InputSource("src/forum17288002/input.xml");
Foo foo2 = (Foo) unmarshaller.unmarshal(inputSource);
outputLocation(inputSource, foo2);
Source source = new StreamSource("src/forum17288002/input.xml");
XMLInputFactory xif = XMLInputFactory.newFactory();
XMLStreamReader xmlStreamReader = xif.createXMLStreamReader(source);
Foo foo3 = (Foo) unmarshaller.unmarshal(xmlStreamReader);
outputLocation(xmlStreamReader, foo3);;
XMLEventReader xmlEventReader = xif.createXMLEventReader(source);
Foo foo4 = (Foo) unmarshaller.unmarshal(xmlEventReader);
outputLocation(xmlEventReader, foo4);
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
Document document = db.parse("src/forum17288002/input.xml");
Foo foo5 = (Foo) unmarshaller.unmarshal(document);
outputLocation(document, foo5);
}
private static void outputLocation(Object input, Foo foo) {
Locator locator = foo.bar.location;
System.out.print(locator.getLineNumber());
System.out.print(" ");
System.out.print(locator.getColumnNumber());
System.out.print(" ");
System.out.println(input.getClass());
}
}
輸出
下面是從運行演示代碼的輸出。唯一沒有產生位置的輸入是DOM輸入。這是有道理的,因爲DOM節點不保存MOXy可以訪問的任何位置信息。
3 11 class java.io.File
3 11 class org.xml.sax.InputSource
3 11 class com.sun.org.apache.xerces.internal.impl.XMLStreamReaderImpl
3 11 class com.sun.xml.internal.stream.XMLEventReaderImpl
0 0 class com.sun.org.apache.xerces.internal.dom.DeferredDocumentImpl
你解開了什麼類型的輸入? –
其實,再次,你讓我:)我認爲它來自一個'節點'。檢查 –
對,所以有一種方法可以將XML加載到DOM中,然後使用XPath選擇'Element',並讓MOXy接受它們。有什麼方法通過DOM隧道'定位器'信息?如果你創建了一個答案,我會接受 –