如何指定JAXB在marshal生成xml时日期的格式

参考文章:[url]http://stackoverflow.com/questions/13568543/how-do-you-specify-the-date-format-used-when-jaxb-marshals-xsddatetime[/url]


方法1:instance级别指定,即每次使用时指定
public static XMLGregorianCalendar getXmlDate(Date date) throws DatatypeConfigurationException {
return DatatypeFactory.newInstance().newXMLGregorianCalendar(new SimpleDateFormat("yyyy-MM-dd").format(date));
}

public static XMLGregorianCalendar getXmlDateTime(Date date) throws DatatypeConfigurationException {
return DatatypeFactory.newInstance().newXMLGregorianCalendar(new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss").format(date));
}


方法2:class级别,即使用Adapter,为一个类的某个属性都使用设置好的日期格式
You can use an XmlAdapter to customize how a date type is written to XML.

import java.text.SimpleDateFormat;
import java.util.Date;

import javax.xml.bind.annotation.adapters.XmlAdapter;

public class DateAdapter extends XmlAdapter<String, Date> {

private SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

@Override
public String marshal(Date v) throws Exception {
return dateFormat.format(v);
}

@Override
public Date unmarshal(String v) throws Exception {
return dateFormat.parse(v);
}

}


Then you use the @XmlJavaTypeAdapter annotation to specify that the XmlAdapter should be used for a specific field/property.

@XmlElement(name = "timestamp", required = true) 
@XmlJavaTypeAdapter(DateAdapter.class)
protected Date timestamp;
在使用JAXB生成XML,如果需要在父节点生成`xmlns:xsi`属性,可以使用`javax.xml.bind.Marshaller.JAXB_SCHEMA_LOCATION`属性来指定XML Schema的位置和命名空间,并通过`javax.xml.bind.Marshaller.setProperty()`方法将该属性设置为要生成XML中的`xmlns:xsi`属性的值。同,需要给父节点添加一个新的属性,属性名为`xmlns:xsi`,属性值为`http://www.w3.org/2001/XMLSchema-instance`。例如: ``` JAXBContext jaxbContext = JAXBContext.newInstance(Root.class); Marshaller marshaller = jaxbContext.createMarshaller(); marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true); marshaller.setProperty(Marshaller.JAXB_SCHEMA_LOCATION, "http://example.com/ns path/to/schema.xsd"); Root root = new Root(); Document doc = DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument(); marshaller.marshal(root, doc); doc.getDocumentElement().setAttributeNS("http://www.w3.org/2000/xmlns/", "xmlns:xsi", "http://www.w3.org/2001/XMLSchema-instance"); Transformer tf = TransformerFactory.newInstance().newTransformer(); tf.setOutputProperty(OutputKeys.INDENT, "yes"); tf.transform(new DOMSource(doc), new StreamResult(System.out)); ``` 在这个例子中,`Marshaller.JAXB_SCHEMA_LOCATION`属性指定XML Schema的位置和命名空间。在生成XMLJAXB会自动将`xsi:schemaLocation`属性添加到生成XML中,并将其值设置为`http://example.com/ns path/to/schema.xsd`。然后,通过`setAttributeNS()`方法给父节点添加一个新的属性`xmlns:xsi`,属性值为`http://www.w3.org/2001/XMLSchema-instance`。最后,使用`Transformer`将生成XML格式输出。例如: ``` <root xmlns="http://example.com/ns" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://example.com/ns path/to/schema.xsd"> ... </root> ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值