English 中文(简体)
日本宇宙航空研究开发机构显示物体含量,但没有为物体添加一分点。
原标题:JAXB displaying an object s content without adding a node for the object

我正在利用日本宇宙航空研究开发机构的说明。 我希望,信用卡内福的展示不会展示出信用卡。 FYI CreditCard 信息是一种复杂的物体。

@XmlRootElement
public Class Notification{
private String notifDate;
private CreditCardInfo ccInfo;
}

public Class CreditCardInfo{
private int ccNum;
private String expiryMonth;
}

希望产出

<notification>
<date>04/29/11</date>
<ccNum>3456</ccNum>
<expiry_month>November</expiry_month>
</notification>

Regards, -Anand

最佳回答

You could use the @XmlPath extension in EclipseLink JAXB (MOXy) to handle this use case. Note: I m the MOXy lead.

import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;

import org.eclipse.persistence.oxm.annotations.XmlPath;

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

    @XmlElement(name="date")
    private String notifDate;

    @XmlPath(".")
    private CreditCardInfo ccInfo;

}

<>CreditCardInfo

import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;

@XmlAccessorType(XmlAccessType.FIELD)
public class CreditCardInfo{

    private int ccNum;

    @XmlElement(name="expiry_month")
    private String expiryMonth;

}

<>Demo

import java.io.File;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.Marshaller;
import javax.xml.bind.Unmarshaller;

public class Demo {

    public static void main(String[] args) throws Exception {
        JAXBContext jc = JAXBContext.newInstance(Notification.class);

        Unmarshaller unmarshaller = jc.createUnmarshaller();
        Notification notification = (Notification) unmarshaller.unmarshal(new File("input.xml"));

        Marshaller marshaller = jc.createMarshaller();
        marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
        marshaller.marshal(notification, System.out);
    }

}

<>strong>jaxb.properties

• 利用MOXy作为你的日本宇宙航空研究开发机构提供商,需要在与你的模型班相同的一揽子方案中添加一个名为“jaxb.properties”的文件,其条目如下:

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

http://www.ohchr.org。

<?xml version="1.0" encoding="UTF-8"?>
<notification>
   <date>04/29/11</date>
   <ccNum>3456</ccNum>
   <expiry_month>November</expiry_month>
</notification>

<>更多信息>

问题回答

暂无回答




相关问题
How to reuse fieldlength in form, validation and ddl?

I m working on an Spring application with lots of input forms. I d like to reuse the field length in the UI-form, validation and JPA annotations. Is there an elegant way to solve this. My solution at ...

MVC Toolkit and XmlMetadataProvider

I have been working on validation with the ASP.NET MVC BETA 2. Some of the reading that I have done is very critical of the buddy class approach because it isn t DRY (Don t Repeat Yourself) and ...

Mapping db-imported countries to address entity with JPA

I ran some DDL script to setup a complete country table in my database. The country table s primary key column contains the corresponding ISO code for every country. In my JPA project I have a User ...

Using annotations to implement a static join in hibernate

I m relatively new to hibernate and was wondering if someone could help me out. While I have no issues implementing a normal join on multiple columns in hibernate using the @JoinColumns tag, I m ...

How to avoid MKMapKit Related Crashes on the iPhone

So, for the past few days I have been struggling to understand how to implement a simple MKMapView with some custom annotations without crashing my application in the process. Unfortunately I have ...

热门标签