1

我有一个 JAXB 类

@XmlTransient
@XmlLocation Locator location;
public Locator getLocation() { return location; }

但是在解组(从 XML)之后,该值为 null。使用 MOXy 2.5.0、JDK 1.7.21。

有什么问题?

4

1 回答 1

1

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

JAVA模型

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

import javax.xml.bind.annotation.*;

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

    Bar bar;

}

酒吧

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

// 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.xml)

<?xml version="1.0" encoding="UTF-8"?>
<foo>
    <bar/>
</foo>

演示代码

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

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
于 2013-06-25T10:27:06.087 回答