gpt4 book ai didi

java - JAXB 使用 namespace 解码..如何?

转载 作者:行者123 更新时间:2023-12-01 16:49:47 25 4
gpt4 key购买 nike

假设我的有效 xml 如下所示:

<A xmlns="something">
<B>4</B>
</A>

我的代码是这样的:

@XmlRootElement(name = "A")
@XmlAccessorType (XmlAccessType.FIELD)
@Getter //from lombok
public class Data{

@XmlElement(name = "B")
private String b;

public Data(String b) {
this.b = b;
}

public Data() {
}
// Getter/setter generated by lombok.
}

我该如何让它发挥作用?如果我的 xml 标记具有一些关联元素,例如上面的“xmlns”,则 JAXB 解码器会抛出异常。

我是第一次这样做,所以我真的对 JAXB、解码等一无所知。我正在挣扎。 :)任何帮助都会很好。

编辑堆栈跟踪:

javax.xml.bind.UnmarshalException: unexpected element (uri:"something", local:"A"). Expected elements are <{}B>,<{}A>
at com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallingContext.handleEvent(UnmarshallingContext.java:726)
at com.sun.xml.internal.bind.v2.runtime.unmarshaller.Loader.reportError(Loader.java:247)
at com.sun.xml.internal.bind.v2.runtime.unmarshaller.Loader.reportError(Loader.java:242)
at com.sun.xml.internal.bind.v2.runtime.unmarshaller.Loader.reportUnexpectedChildElement(Loader.java:109)
at com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallingContext$DefaultRootLoader.childElement(UnmarshallingContext.java:1131)
at com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallingContext._startElement(UnmarshallingContext.java:556)
at com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallingContext.startElement(UnmarshallingContext.java:538)
at com.sun.xml.internal.bind.v2.runtime.unmarshaller.SAXConnector.startElement(SAXConnector.java:153)
at com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser.startElement(AbstractSAXParser.java:509)
at com.sun.org.apache.xerces.internal.impl.XMLNSDocumentScannerImpl.scanStartElement(XMLNSDocumentScannerImpl.java:374)
at com.sun.org.apache.xerces.internal.impl.XMLNSDocumentScannerImpl$NSContentDriver.scanRootElementHook(XMLNSDocumentScannerImpl.java:613)
at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl$FragmentContentDriver.next(XMLDocumentFragmentScannerImpl.java:3132)
at com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl$PrologDriver.next(XMLDocumentScannerImpl.java:852)
at com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl.next(XMLDocumentScannerImpl.java:602)
at com.sun.org.apache.xerces.internal.impl.XMLNSDocumentScannerImpl.next(XMLNSDocumentScannerImpl.java:112)
at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl.scanDocument(XMLDocumentFragmentScannerImpl.java:505)
at ...

最佳答案

来自 XmlRootElement 的来源:

public @interface XmlRootElement {
/**
* namespace name of the XML element.
* <p>
* If the value is "##default", then the XML namespace name is derived
* from the package of the class ( {@link XmlSchema} ). If the
* package is unnamed, then the XML namespace is the default empty
* namespace.
*/
String namespace() default "##default";
// else of file

您还应该记住,此规则也适用于大多数 JAXB 注释,因此您需要指定根元素以及其他元素并显式给出命名空间,如下所示:

@XmlRootElement(namespace="something", name = "A")
@XmlAccessorType (XmlAccessType.FIELD)
@Getter //from lombok
public class Data{
@XmlElement(namespace="something", name = "B") // <-- mind this
private String b;

public Data(String b) {
this.b = b;
}

public Data() {
}
// Getter/setter generated by lombok.
}

如果您不这样做,假设您的 Data 位于 org.example 包中,它会期望命名空间 org.example 中的一些元素> 只要您有@XmlSchema(elementFormDefault=QUALIFIED),否则它将需要默认命名空间中的元素。

关于java - JAXB 使用 namespace 解码..如何?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42874676/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com