我正在尝试使用创建 XMLJaxB Marshalling
方法。我想跳过某些子项的父标签,或者可能添加新的XML
某个元素的父标签。因此我尝试使用@XmlPath
from import org.eclipse.persistence.oxm.annotations.XmlPath;
.
我正在关注创始人的博客(@XmlPath 上的博客 http://blog.bdoughan.com/2010/07/xpath-based-mapping.html)使其工作,但由于某种原因,@XmlPath
没有影响,并且输出 XML 与博客中所示不匹配。我遵循了博客中提到的所有过程,并在这里提到了各种答案。
我所有的课程都在model.jaxb
包裹。另外,我还创建了一个jaxb.properties
该包中的文件。
以下是我的Customer
class:
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlRootElement;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import org.eclipse.persistence.oxm.annotations.XmlPath;
@Getter
@Setter
@NoArgsConstructor
@XmlRootElement
@XmlAccessorType(XmlAccessType.FIELD)
public class Customer {
@XmlPath("contact-info/billing-address")
private Address billingAddress;
@XmlPath("contact-info/shipping-address")
private Address shippingAddress;
}
以下是我的Address
class:
@Getter
@Setter
@NoArgsConstructor
public class Address {
private String street;
}
以下是Demo
class:
public class Demo {
public static void main(String[] args) throws Exception {
JAXBContext jc = JAXBContext.newInstance(Customer.class);
Customer customer = new Customer();
Address billingAddress = new Address();
billingAddress.setStreet("1 Billing Street");
customer.setBillingAddress(billingAddress);
Address shippingAddress = new Address();
shippingAddress.setStreet("2 Shipping Road");
customer.setShippingAddress(shippingAddress);
Marshaller m = jc.createMarshaller();
m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
m.marshal(customer, System.out);
}
}
我得到的 XML 有和没有@XmlPath
实际上是一样的。所以看起来像@XmlPath
在编组过程中没有影响。
<customer>
<billingAddress>
<street>1 Billing Street</street>
</billingAddress>
<shippingAddress>
<street>2 Shipping Road</street>
</shippingAddress>
</customer>
我创造了jaxb.properties
文件在我的package model.jaxb
内容:
javax.xml.bind.context.factory=org.eclipse.persistence.jaxb.JAXBContextFactory
根据博客,输出 XML 应该是这样的:
<customer>
<contact-info>
<billing-address>
<street>1 Billing Street</street>
</billing-address>
<shipping-address>
<street>2 Shipping Road</street>
</shipping-address>
</contact-info>
<customer>
我不确定我做错了什么,因为我无法按预期获取 XML。我尝试了很多方法,但没有任何效果,所以在这里发布相同的内容。
有人可以帮助我如何制作@XmlPath
在编组期间踢以便我可以在编组期间获得预期的标签?
**** 已编辑 *****
我正在使用 Java 版本 15.0.01Intellij IDE File -> Project Structure
。我看到一个答案提到了Moxy
直接在上面不起作用Java 11
回答Java 11.0以上 https://stackoverflow.com/a/60285555/7584240.
所以我做了以下事情:
- 我添加了依赖项并
build
正如上面的答案中提到的,所以我的pom.xml
看起来像这样:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>model.jaxb</groupId>
<version>1.0-SNAPSHOT</version>
<artifactId>model-jaxb</artifactId>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>8</source>
<target>8</target>
</configuration>
</plugin>
</plugins>
<resources>
<resource>
<directory>src/main/java</directory>
<excludes>
<exclude>**/*.java</exclude>
</excludes>
</resource>
</resources>
</build>
<dependencies>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.16</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>com.sun.activation</groupId>
<artifactId>javax.activation</artifactId>
<version>1.2.0</version>
</dependency>
<dependency>
<groupId>javax.xml.bind</groupId>
<artifactId>jaxb-api</artifactId>
<version>2.3.1</version>
</dependency>
<dependency>
<groupId>com.sun.xml.bind</groupId>
<artifactId>jaxb-core</artifactId>
<version>2.3.0.1</version>
</dependency>
<dependency>
<groupId>com.sun.xml.bind</groupId>
<artifactId>jaxb-impl</artifactId>
<version>2.3.1</version>
</dependency>
<dependency>
<groupId>org.eclipse.persistence</groupId>
<artifactId>eclipselink</artifactId>
<version>2.7.6</version>
</dependency>
<dependency>
<groupId>jakarta.xml.bind</groupId>
<artifactId>jakarta.xml.bind-api</artifactId>
<version>2.3.2</version>
</dependency>
<dependency>
<groupId>org.glassfish.jaxb</groupId>
<artifactId>jaxb-runtime</artifactId>
<version>2.3.2</version>
</dependency>
</dependencies>
</project>
当我运行该程序时,我收到错误:Exception in thread "main" java.lang.NoClassDefFoundError: jakarta/xml/bind/JAXBException
Caused by: java.lang.ClassNotFoundException: jakarta.xml.bind.JAXBException
我尝试了很多上面提到的事情StackOverflow for this error and most of the answers mentioned adding the
dependency`我已经添加了答案中提到的所有依赖项。我不确定出了什么问题。
有人可以帮我解决这个问题吗?