gpt4 book ai didi

java - MOXy Dynamix JAXB 上下文解码到错误的属性名称

转载 作者:太空宇宙 更新时间:2023-11-04 15:23:19 24 4
gpt4 key购买 nike

我目前正在尝试使用 MOXy 中的 DynamicJaxbContext 解码以下 XML 文件:

<?xml version="1.0" encoding="UTF-8"?>
<request xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="http://somehost.com/schema_schedule_request.xsd">
<header>
<originator>MySatelliteCompany</originator>
<recipient>Recipient</ recipient>
<schedule_valid_from>2008-12-17T00:00:00Z</schedule_valid_from>
<schedule_valid_to>2008-12-18T00:00:07Z</schedule_valid_to>
<request_reference>HGT4285T3</request_reference>
<generation_time>2008-12-16T08:24:00Z</generation_time>
</header>
<body>
<schedule_request>
<start_time>2008-12-17T09:30:47Z</start_time>
<end_time>2008-12-17T09:33:47Z</end_time>
<satellite_name>MySat</satellite_name>
</schedule_request>
</body>
</request>

它可以工作,但动态创建的 Java 类的属性与 XML 中给出的字段不对应。例如:<satellite_name>被解码为“satelliteName”。这使得为​​我的后端 API 编写自定义绑定(bind)文件变得非常困难,因为我必须首先解码我将作为输入获得的所有 XML 文件并手动写下相应的属性名称,或者编写另一个帮助程序应用程序来为我执行此操作。

是否有任何方法可以更改此 MOXy 行为,以便它按照 XML 中的方式正确解码字段名称?

补充:所以我在 MOXy 文档中找到了原因:

XML names found in the metadata (complex type names, element names, attribute names) will be translated to Java identifiers according to the algorithms described in "Appendix D: Binding XML Names to Java Identifiers" of the Java Architecture for XML Binding (JAXB) 2.2 Specification (http://jcp.org/en/jsr/detail?id=222). - See more at: http://www.eclipse.org/eclipselink/documentation/2.4/moxy/dynamic_jaxb001.htm#BABCDJDF

但我的原则问题仍然存在:有什么方法可以关闭此功能或修改此行为吗?

最佳答案

您的ADDITION是正确的,MOXy 不会解码错误的属性名称,它只是解码到与生成的类中映射的属性/字段名称相对应的属性名称。

解决方案应该是什么

绑定(bind).xml

JAXB 中默认的 XML 模式到 Java 命名规则是删除 _。您可以提供一个绑定(bind)文件以使此行为有所不同。

<jaxb:bindings 
xmlns:jaxb="http://java.sun.com/xml/ns/jaxb"
version="2.1">
<jaxb:globalBindings underscoreBinding="asCharInWord"/>
</jaxb:bindings>

演示

使用 MOXy 的 Dynamic JAXB,下面是如何利用绑定(bind)文件的示例。

import java.io.File;
import java.util.*;
import javax.xml.bind.*;
import javax.xml.transform.stream.StreamSource;
import org.eclipse.persistence.dynamic.DynamicEntity;
import org.eclipse.persistence.jaxb.dynamic.DynamicJAXBContextFactory;

public class Demo {

public static void main(String[] args) throws Exception {
StreamSource schemaSource = new StreamSource("src/forum20146935/schema.xsd");
Map<String, Object> properties = new HashMap<String, Object>(1);
properties.put(DynamicJAXBContextFactory.EXTERNAL_BINDINGS_KEY, new StreamSource("src/forum20146935/binding.xml"));
JAXBContext jc = DynamicJAXBContextFactory.createContextFromXSD(schemaSource, null, Demo.class.getClassLoader(), properties);

Unmarshaller unmarshaller = jc.createUnmarshaller();
File xml = new File("src/forum20146935/input.xml");
DynamicEntity root = (DynamicEntity) unmarshaller.unmarshal(xml);
System.out.println(root.get("foo_bar"));
}

}

为什么不起作用?

正如我之前提到的,MOXy 将根据 XJC 生成的相应映射字段/属性来生成动态属性名称。这看起来就像属性名称具有 _ 但相应的映射字段没有。

@XmlElement(name = "foo_bar", required = true)
protected String fooBar;

public String getFoo_Bar() {
return fooBar;
}

public void setFoo_Bar(String value) {
this.fooBar = value;
}

你可以做什么

  1. 使用转换后的键名称。
  2. 您可以使用 MOXy JAXBContext 上的 getValueByXPath 功能以更像 XML 的方式与对象进行交互。

    import java.io.File;
    import java.util.*;

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

    import org.eclipse.persistence.dynamic.DynamicEntity;
    import org.eclipse.persistence.jaxb.JAXBHelper;
    import org.eclipse.persistence.jaxb.dynamic.DynamicJAXBContextFactory;

    public class Demo {

    public static void main(String[] args) throws Exception {
    StreamSource schemaSource = new StreamSource("src/forum20146935/schema.xsd");
    Map<String, Object> properties = new HashMap<String, Object>(1);
    properties.put(DynamicJAXBContextFactory.EXTERNAL_BINDINGS_KEY, new StreamSource("src/forum20146935/binding.xml"));
    JAXBContext jc = DynamicJAXBContextFactory.createContextFromXSD(schemaSource, null, Demo.class.getClassLoader(), properties);

    Unmarshaller unmarshaller = jc.createUnmarshaller();
    File xml = new File("src/forum20146935/input.xml");
    DynamicEntity root = (DynamicEntity) unmarshaller.unmarshal(xml);

    String fooBar = JAXBHelper.getJAXBContext(jc).getValueByXPath(root, "foo_bar/text()", null, String.class);
    System.out.println(fooBar);
    }

    }

关于java - MOXy Dynamix JAXB 上下文解码到错误的属性名称,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20146935/

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