MOXy XPath解组的元素为空。

huangapple go评论115阅读模式
英文:

MOXy XPath unmarshalling Element is null

问题

我正在尝试使用XPath将camunda:property元素解组为List,以跳过不必要的包装元素。不幸的是,我的propertyList始终为null。这位于Task类中。非常感谢任何帮助。

编辑#1:
我按照以下链接进行了操作,这些链接应该有助于解决我的问题,但不幸的是没有成功。
http://blog.bdoughan.com/2011/05/specifying-eclipselink-moxy-as-your.html,这是官方指南。显然,在maven pom.xml文件中存在一些挑战。我怀疑问题出在pom文件中。
我遵循了这个指南https://www.javacodegeeks.com/2012/07/eclipselink-moxy-as-jaxb-provider.html,但仍然无法使其工作。

pom.xml文件

  1. <!-- 在这里是您的 pom.xml 内容 -->

jaxb.properties文件与我的java类位于相同的包和文件夹中(请参见附带的名称为“Project Structure”的图像)。

  1. javax.xml.bind.context.factory=org.eclipse.persistence.jaxb.JAXBContextFactory

package-info.java文件

  1. /* 在这里是您的 package-info.java 内容 */

xml文件片段

  1. <!-- 在这里是您的 xml 文件片段 -->

Definitions类

  1. /* 在这里是您的 Definitions 类内容 */

Process类

  1. /* 在这里是您的 Process 类内容 */

Task类

  1. /* 在这里是您的 Task 类内容 */

Property类

  1. /* 在这里是您的 Property 类内容 */

Main类

  1. /* 在这里是您的 Main 类内容 */

项目结构

英文:

Im trying to unmarshall the camunda:property elements into a List using XPath to skip the unnecessary wrapper elements. Unfortunately my propertyList is always null. This is located in the Task Class. Any help would be greatly appreciated.

Edit#1:
I followed the following links who were supposed to help with my problem unfortunately without success. http://blog.bdoughan.com/2011/05/specifying-eclipselink-moxy-as-your.html which is the official guide. Apparently there are some challenges with the maven pom.xml file. I suspect that the problem lies inside the pom file.
I followed this guide https://www.javacodegeeks.com/2012/07/eclipselink-moxy-as-jaxb-provider.html but still did not get it to work.

pom.xml file

  1. &lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;
  2. &lt;project xmlns=&quot;http://maven.apache.org/POM/4.0.0&quot;
  3. xmlns:xsi=&quot;http://www.w3.org/2001/XMLSchema-instance&quot;
  4. xsi:schemaLocation=&quot;http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd&quot;&gt;
  5. &lt;modelVersion&gt;4.0.0&lt;/modelVersion&gt;
  6. &lt;groupId&gt;org.example&lt;/groupId&gt;
  7. &lt;artifactId&gt;BPMN-Marshaller&lt;/artifactId&gt;
  8. &lt;version&gt;1.0-SNAPSHOT&lt;/version&gt;
  9. &lt;properties&gt;
  10. &lt;maven.compiler.source&gt;1.8&lt;/maven.compiler.source&gt;
  11. &lt;maven.compiler.target&gt;1.8&lt;/maven.compiler.target&gt;
  12. &lt;/properties&gt;
  13. &lt;repositories&gt;&lt;repository&gt;
  14. &lt;id&gt;EclipseLink Repo&lt;/id&gt;
  15. &lt;url&gt;http://download.eclipse.org/rt/eclipselink/maven.repo&lt;/url&gt;
  16. &lt;name&gt;EclipseLink Repo&lt;/name&gt;
  17. &lt;/repository&gt;&lt;/repositories&gt;
  18. &lt;dependencies&gt;
  19. &lt;dependency&gt;
  20. &lt;groupId&gt;org.eclipse.persistence&lt;/groupId&gt;
  21. &lt;artifactId&gt;org.eclipse.persistence.moxy&lt;/artifactId&gt;
  22. &lt;version&gt;3.0.0-M1&lt;/version&gt;
  23. &lt;/dependency&gt;
  24. &lt;dependency&gt;
  25. &lt;groupId&gt;com.fasterxml.jackson.core&lt;/groupId&gt;
  26. &lt;artifactId&gt;jackson-databind&lt;/artifactId&gt;
  27. &lt;version&gt;2.5.3&lt;/version&gt;
  28. &lt;/dependency&gt;
  29. &lt;!-- Runtime, com.sun.xml.bind module --&gt;
  30. &lt;dependency&gt;
  31. &lt;groupId&gt;org.glassfish.jaxb&lt;/groupId&gt;
  32. &lt;artifactId&gt;jaxb-runtime&lt;/artifactId&gt;
  33. &lt;version&gt;2.3.2&lt;/version&gt;
  34. &lt;/dependency&gt;
  35. &lt;dependency&gt;
  36. &lt;groupId&gt;jakarta.xml.bind&lt;/groupId&gt;
  37. &lt;artifactId&gt;jakarta.xml.bind-api&lt;/artifactId&gt;
  38. &lt;version&gt;2.3.2&lt;/version&gt;
  39. &lt;/dependency&gt;
  40. &lt;/dependencies&gt;
  41. &lt;/project&gt;

jaxb.properties file in the same package and folder as my java classes(see attached image with name "Project Structure")

  1. javax.xml.bind.context.factory=org.eclipse.persistence.jaxb.JAXBContextFactory

package-info.java file

  1. @XmlSchema(namespace=&quot;http://www.omg.org/spec/BPMN/20100524/MODEL&quot;, elementFormDefault=XmlNsForm.QUALIFIED, xmlns = {@XmlNs(prefix=&quot;bpmn&quot;, namespaceURI=&quot;http://www.omg.org/spec/BPMN/20100524/MODEL&quot;)
  2. ,@XmlNs(prefix = &quot;camunda&quot;, namespaceURI = &quot;http://camunda.org/schema/1.0/bpmn&quot;)})
  3. package bpmn;
  4. import javax.xml.bind.annotation.*;

xml file snippet

  1. &lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;
  2. &lt;bpmn:definitions xmlns:bpmn=&quot;http://www.omg.org/spec/BPMN/20100524/MODEL&quot; xmlns:bpmndi=&quot;http://www.omg.org/spec/BPMN/20100524/DI&quot; xmlns:dc=&quot;http://www.omg.org/spec/DD/20100524/DC&quot; xmlns:camunda=&quot;http://camunda.org/schema/1.0/bpmn&quot; xmlns:di=&quot;http://www.omg.org/spec/DD/20100524/DI&quot; id=&quot;Definitions_13d3a6z&quot; targetNamespace=&quot;http://bpmn.io/schema/bpmn&quot; exporter=&quot;Camunda Modeler&quot; exporterVersion=&quot;4.1.1&quot;&gt;
  3. &lt;bpmn:process id=&quot;Process_1tovjba&quot; isExecutable=&quot;true&quot;&gt;
  4. &lt;bpmn:startEvent id=&quot;StartEvent_1&quot;&gt;
  5. &lt;bpmn:outgoing&gt;Flow_06i118e&lt;/bpmn:outgoing&gt;
  6. &lt;/bpmn:startEvent&gt;
  7. &lt;bpmn:task id=&quot;Activity_1d3friu&quot; name=&quot;Task 1&quot;&gt;
  8. &lt;bpmn:extensionElements&gt;
  9. &lt;camunda:properties&gt;
  10. &lt;camunda:property name=&quot;start_date&quot; value=&quot;01-04-2018&quot; /&gt;
  11. &lt;camunda:property name=&quot;duration&quot; value=&quot;5&quot; /&gt;
  12. &lt;/camunda:properties&gt;
  13. &lt;/bpmn:extensionElements&gt;
  14. &lt;bpmn:incoming&gt;Flow_06i118e&lt;/bpmn:incoming&gt;
  15. &lt;bpmn:outgoing&gt;Flow_0linmbs&lt;/bpmn:outgoing&gt;
  16. &lt;/bpmn:task&gt;

Definitions Class

  1. @XmlRootElement
  2. public class Definitions {
  3. private String id;
  4. private Process process;
  5. public Definitions(){};
  6. public Definitions(String id, Process process){
  7. super();
  8. this.id = id;
  9. this.process = process;
  10. }
  11. @XmlAttribute
  12. public String getId() {
  13. return id;
  14. }
  15. public void setId(String id) {
  16. this.id = id;
  17. }
  18. @XmlElement
  19. public Process getProcess() {
  20. return process;
  21. }
  22. public void setProcess(Process process) {
  23. this.process = process;
  24. }
  25. @Override
  26. public String toString(){
  27. return &quot;Definitions [id23=&quot; + id + &quot;, process=23499999999999999&quot; + process + &quot;]&quot;;
  28. }
  29. }

Process Class

  1. public class Process {
  2. private String id;
  3. private List&lt;Task&gt; taskList;
  4. private List&lt;SequenceFlow&gt; sequenceFlowList;
  5. public Process(){};
  6. public Process(String id, List&lt;Task&gt; taskList, List&lt;SequenceFlow&gt; sequenceFlowList){
  7. super();
  8. this.id = id;
  9. this.taskList = taskList;
  10. this.sequenceFlowList = sequenceFlowList;
  11. }
  12. @XmlAttribute
  13. public String getId() {
  14. return id;
  15. }
  16. public void setId(String id) {
  17. this.id = id;
  18. }
  19. @XmlElement(name = &quot;task&quot;)
  20. public List&lt;Task&gt; getTaskList() {
  21. return taskList;
  22. }
  23. public void setTaskList(List&lt;Task&gt; taskList) {
  24. this.taskList = taskList;
  25. }
  26. @XmlElement(name = &quot;sequenceFlow&quot;)
  27. public List&lt;SequenceFlow&gt; getSequenceFlowList() {
  28. return sequenceFlowList;
  29. }
  30. public void setSequenceFlowList(List&lt;SequenceFlow&gt; sequenceFlowList) {
  31. this.sequenceFlowList = sequenceFlowList;
  32. }
  33. }

Task Class

  1. public class Task {
  2. private String id;
  3. private String name;
  4. private List&lt;Property&gt; propertyList;
  5. public Task(){}
  6. public Task(String id, String name, List&lt;Property&gt; propertyList){
  7. super();
  8. this.id = id;
  9. this.name = name;
  10. this.propertyList = propertyList;
  11. }
  12. @XmlAttribute
  13. @JsonProperty(&quot;text&quot;)
  14. public String getName() {
  15. return name;
  16. }
  17. public void setName(String name) {
  18. this.name = name;
  19. }
  20. @XmlAttribute
  21. public String getId() {
  22. return id;
  23. }
  24. public void setId(String id) {
  25. this.id = id;
  26. }
  27. @XmlElement(name = &quot;property&quot;)
  28. @XmlPath(&quot;bpmn:extensionElements/camunda:properties/camunda:property&quot;)
  29. public List&lt;Property&gt; getPropertyList() {
  30. return propertyList;
  31. }
  32. public void setPropertyList(List&lt;Property&gt; propertyList) {
  33. this.propertyList = propertyList;
  34. }
  35. }

Property Class

  1. public class Property {
  2. private String name;
  3. private String value;
  4. public Property(){}
  5. public Property(String name, String value) {
  6. super();
  7. this.name = name;
  8. this.value = value;
  9. }
  10. @XmlAttribute
  11. public String getName() {
  12. return name;
  13. }
  14. public void setName(String name) {
  15. this.name = name;
  16. }
  17. @XmlAttribute
  18. public String getValue() {
  19. return value;
  20. }
  21. public void setValue(String value) {
  22. this.value = value;
  23. }
  24. }

Main Class

  1. public class XMLToObject {
  2. public static void main(String[] args) {
  3. try {
  4. File file = new File(&quot;process.bpmn&quot;);
  5. JAXBContext jaxbContext = JAXBContext.newInstance(Definitions.class);
  6. Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
  7. Definitions definitions = (Definitions) jaxbUnmarshaller.unmarshal(file);
  8. System.out.println(definitions.getProcess().getTaskList().get(0).getPropertyList());
  9. } catch (JAXBException e) {
  10. e.printStackTrace();
  11. }
  12. }
  13. }

Project Structure

答案1

得分: 1

我对你的方法做了以下更改,我能够从你的XML文件中访问durationstart_date属性的数据。

顺便说一下,我正在使用OpenJDK 14。但这种方法也可以在版本8上运行正常。

我正在使用的POM具有以下依赖关系:

  1. <dependency>
  2. <groupId>com.sun.activation</groupId>
  3. <artifactId>javax.activation</artifactId>
  4. <version>1.2.0</version>
  5. </dependency>
  6. <!--
  7. 请使用2.3.1以避免“非法反射访问操作”警告。
  8. -->
  9. <dependency>
  10. <groupId>javax.xml.bind</groupId>
  11. <artifactId>jaxb-api</artifactId>
  12. <version>2.3.1</version>
  13. </dependency>
  14. <dependency>
  15. <groupId>com.sun.xml.bind</groupId>
  16. <artifactId>jaxb-core</artifactId>
  17. <version>2.3.0.1</version>
  18. </dependency>
  19. <dependency>
  20. <groupId>com.sun.xml.bind</groupId>
  21. <artifactId>jaxb-impl</artifactId>
  22. <version>2.3.1</version>
  23. </dependency>
  24. <dependency>
  25. <groupId>org.eclipse.persistence</groupId>
  26. <artifactId>eclipselink</artifactId>
  27. <version>2.7.6</version>
  28. </dependency>

(为了这个测试,我跳过了Jackson依赖项。)

我还在我的POM末尾添加了以下部分,以处理属性文件:

  1. <!-- 复制jaxb.properties文件到其类包中的位置: -->
  2. <build>
  3. <resources>
  4. <resource>
  5. <directory>src/main/java</directory>
  6. <excludes>
  7. <exclude>**/*.java</exclude>
  8. </excludes>
  9. </resource>
  10. </resources>
  11. </build>

这确保属性文件与其相关的类文件一起部署到正确的位置。

我添加了用于检查正在使用的JAXB提供程序的代码 - 只是作为积极的确认:

  1. private void checkProvider() throws JAXBException {
  2. JAXBContext jc = JAXBContext.newInstance(Definitions.class);
  3. String jaxbContextImpl = jc.getClass().getName();
  4. if(MOXY_JAXB_CONTEXT.equals(jaxbContextImpl)) {
  5. System.out.println("EclipseLink MOXy");
  6. } else if(METRO_JAXB_CONTEXT.equals(jaxbContextImpl)) {
  7. System.out.println("Metro");
  8. } else {
  9. System.out.println("Other");
  10. }
  11. }

我修改了代码以循环遍历属性数据,以显式打印最终的属性值:

  1. List<Property> props = definitions.getProcess().getTaskList().get(0).getPropertyList();
  2. props.forEach(prop -> {
  3. System.out.println(prop.getName() + " - " + prop.getValue());
  4. });

结果输出为:

  1. EclipseLink MOXy
  2. start_date - 01-04-2018
  3. duration - 5
英文:

I made the following changes to your approach, and I am able to access the duration and start_date properties data from your XML file.

I am using OpenJDK 14, by the way. But this approach runs OK using version 8 also.

The POM I am using has the following dependencies:

  1. &lt;dependency&gt;
  2. &lt;groupId&gt;com.sun.activation&lt;/groupId&gt;
  3. &lt;artifactId&gt;javax.activation&lt;/artifactId&gt;
  4. &lt;version&gt;1.2.0&lt;/version&gt;
  5. &lt;/dependency&gt;
  6. &lt;!--
  7. Use 2.3.1 below to prevent &quot;illegal
  8. reflective access operation&quot; warnings.
  9. --&gt;
  10. &lt;dependency&gt;
  11. &lt;groupId&gt;javax.xml.bind&lt;/groupId&gt;
  12. &lt;artifactId&gt;jaxb-api&lt;/artifactId&gt;
  13. &lt;version&gt;2.3.1&lt;/version&gt;
  14. &lt;/dependency&gt;
  15. &lt;dependency&gt;
  16. &lt;groupId&gt;com.sun.xml.bind&lt;/groupId&gt;
  17. &lt;artifactId&gt;jaxb-core&lt;/artifactId&gt;
  18. &lt;version&gt;2.3.0.1&lt;/version&gt;
  19. &lt;/dependency&gt;
  20. &lt;dependency&gt;
  21. &lt;groupId&gt;com.sun.xml.bind&lt;/groupId&gt;
  22. &lt;artifactId&gt;jaxb-impl&lt;/artifactId&gt;
  23. &lt;version&gt;2.3.1&lt;/version&gt;
  24. &lt;/dependency&gt;
  25. &lt;dependency&gt;
  26. &lt;groupId&gt;org.eclipse.persistence&lt;/groupId&gt;
  27. &lt;artifactId&gt;eclipselink&lt;/artifactId&gt;
  28. &lt;version&gt;2.7.6&lt;/version&gt;
  29. &lt;/dependency&gt;

(I skipped the Jackson dependency just for this test.)

I also added the following section at the end of my POM, to handle the properties file:

  1. &lt;!-- to copy the jaxb.properties file to its class package: --&gt;
  2. &lt;build&gt;
  3. &lt;resources&gt;
  4. &lt;resource&gt;
  5. &lt;directory&gt;src/main/java&lt;/directory&gt;
  6. &lt;excludes&gt;
  7. &lt;exclude&gt;**/*.java&lt;/exclude&gt;
  8. &lt;/excludes&gt;
  9. &lt;/resource&gt;
  10. &lt;/resources&gt;
  11. &lt;/build&gt;

This ensures the properties file is deployed to the correct location with its related class files.

I added the code to check for which JAXB provider is being used - just as a positive confirmation:

  1. private void checkProvider() throws JAXBException {
  2. JAXBContext jc = JAXBContext.newInstance(Definitions.class);
  3. String jaxbContextImpl = jc.getClass().getName();
  4. if(MOXY_JAXB_CONTEXT.equals(jaxbContextImpl)) {
  5. System.out.println(&quot;EclipseLink MOXy&quot;);
  6. } else if(METRO_JAXB_CONTEXT.equals(jaxbContextImpl)) {
  7. System.out.println(&quot;Metro&quot;);
  8. } else {
  9. System.out.println(&quot;Other&quot;);
  10. }
  11. }

I modified the code to loop through the properties data, to explicitly print the final properties values:

  1. List&lt;Property&gt; props = definitions.getProcess().getTaskList().get(0).getPropertyList();
  2. props.forEach(prop -&gt; {
  3. System.out.println(prop.getName() + &quot; - &quot; + prop.getValue());
  4. });
  5. //System.out.println(definitions.getProcess().getTaskList().get(0).getPropertyList());

The resulting output is:

  1. EclipseLink MOXy
  2. start_date - 01-04-2018
  3. duration - 5

huangapple
  • 本文由 发表于 2020年8月18日 20:41:52
  • 转载请务必保留本文链接:https://go.coder-hub.com/63468873.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定