JAXB/MOXy:@XmlLocation定位器不';行不通

JAXB/MOXy:@XmlLocation定位器不';行不通,jaxb,location,moxy,Jaxb,Location,Moxy,我有一个JAXB类 @XmlTransient @XmlLocation Locator location; public Locator getLocation() { return location; } 但是(从XML)解组后,该值为null。 使用MOXy 2.5.0、JDK 1.7.21 有什么问题吗?的@XmlLocation确实有效,但如果您从DOM节点解组,则它将不会捕获任何位置信息。下面我将用一个例子来演示 JAVA模型 下面是我们将用于此示例的Java模型 Foo impo

我有一个JAXB类

@XmlTransient
@XmlLocation Locator location;
public Locator getLocation() { return location; }
但是(从XML)解组后,该值为null。 使用MOXy 2.5.0、JDK 1.7.21

有什么问题吗?

@XmlLocation
确实有效,但如果您从DOM
节点
解组,则它将不会捕获任何位置信息。下面我将用一个例子来演示

JAVA模型

下面是我们将用于此示例的Java模型

Foo

import javax.xml.bind.annotation.*;
@XmlRootElement
@XmlAccessorType(XmlAccessType.FIELD)
公开课Foo{
酒吧;
}

我们将使用
Bar
类上的
@XmlLocation
注释来存储位置。MOXy支持来自JAXB参考实现的
@XmlLocation
注释(包括内部包中重新打包的注释)及其自己的版本

//导入com.sun.xml.bind.annotation.XmlLocation;
//导入com.sun.xml.internal.bind.annotation.XmlLocation;
导入javax.xml.bind.annotation.*;
导入org.eclipse.persistence.oxm.annotations.XmlLocation;
导入org.xml.sax.Locator;
@XmlAccessorType(XmlAccessType.FIELD)
公共类酒吧{
@XmlTransient
@XmlLocation
定位器位置;
}
XML输入(INPUT.XML)


演示代码

下面是一些演示代码,将解组不同类型的输入,然后输出位置

导入java.io.File;
导入javax.xml.bind.*;
导入javax.xml.parsers.*;
导入javax.xml.stream.*;
导入javax.xml.transform.Source;
导入javax.xml.transform.stream.StreamSource;
导入org.w3c.dom.Document;
导入org.xml.sax.*;
公开课演示{
公共静态void main(字符串[]args)引发异常{
JAXBContext jc=JAXBContext.newInstance(Foo.class);
Unmarshaller Unmarshaller=jc.createUnmarshaller();
File File=新文件(“src/forum1728802/input.xml”);
Foo foo1=(Foo)unmarshaller.unmarshal(文件);
输出位置(文件,foo1);
InputSource InputSource=新的InputSource(“src/forum1728802/input.xml”);
Foo foo2=(Foo)解组器.解组器(inputSource);
输出位置(输入源,foo2);
Source Source=newstreamsource(“src/forum1728802/input.xml”);
XMLInputFactory xif=XMLInputFactory.newFactory();
XMLStreamReader=xif.createXMLStreamReader(源);
Foo foo3=(Foo)unmarshaller.unmarshal(xmlStreamReader);
输出位置(xmlStreamReader,foo3);;
XMLEventReader XMLEventReader=xif.createXMLEventReader(源);
Foo foo4=(Foo)unmarshaller.unmarshal(xmlEventReader);
输出位置(xmlEventReader,foo4);
DocumentBuilderFactory dbf=DocumentBuilderFactory.newInstance();
DocumentBuilder db=dbf.newDocumentBuilder();
documentdocument=db.parse(“src/forum1728802/input.xml”);
Foo foo5=(Foo)解组器。解组器(文档);
输出位置(文件,foo5);
}
私有静态void outputLocation(对象输入,Foo-Foo){
定位器定位器=foo.bar.location;
System.out.print(locator.getLineNumber());
系统输出打印(“”);
System.out.print(locator.getColumnNumber());
系统输出打印(“”);
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
@XmlLocation
确实有效,但如果您从DOM
节点
解组,则它将不会捕获任何位置信息。下面我将用一个例子来演示

JAVA模型

下面是我们将用于此示例的Java模型

Foo

import javax.xml.bind.annotation.*;
@XmlRootElement
@XmlAccessorType(XmlAccessType.FIELD)
公开课Foo{
酒吧;
}

我们将使用
Bar
类上的
@XmlLocation
注释来存储位置。MOXy支持来自JAXB参考实现的
@XmlLocation
注释(包括内部包中重新打包的注释)及其自己的版本

//导入com.sun.xml.bind.annotation.XmlLocation;
//导入com.sun.xml.internal.bind.annotation.XmlLocation;
导入javax.xml.bind.annotation.*;
导入org.eclipse.persistence.oxm.annotations.XmlLocation;
导入org.xml.sax.Locator;
@XmlAccessorType(XmlAccessType.FIELD)
公共类酒吧{
@XmlTransient
@XmlLocation
定位器位置;
}
XML输入(INPUT.XML)


演示代码

下面是一些演示代码,将解组不同类型的输入,然后输出位置

导入java.io.File;
导入javax.xml.bind.*;
导入javax.xml.parsers.*;
导入javax.xml.stream.*;
导入javax.xml.transform.Source;
导入javax.xml.transform.stream.StreamSource;
导入org.w3c.dom.Document;
导入org.xml.sax.*;
公开课演示{
公共静态void main(字符串[]args)引发异常{
JAXBContext jc=JAXBContext.newInstance(Foo.class);
Unmarshaller Unmarshaller=jc.createUnmarshaller();
File File=新文件(“src/forum1728802/input.xml”);
Foo foo1=(Foo)unmarshaller.unmarshal(文件);
输出位置(文件,foo1);
InputSource InputSource=新的InputSource(“src/forum1728802/input.xml”);
Foo foo2=(Foo)解组器.解组器(inputSource);
输出位置(输入源,foo2);
Source Source=newstreamsource(“src/forum1728802/input.xml”);
XMLInputFactory xif=XMLInputFactory。