【发布时间】:2012-03-08 07:20:58
【问题描述】:
我正在编写一个没有 XSD 编译的 JAXB 类。
谁能告诉我两者的区别
@XmlSchemaType(name = "normalizedString")
private String normalized;
和
@XmlJavaTypeAdapter(NormalizedStringAdapter.class)
private String normalized;
?
【问题讨论】:
标签: jaxb
我正在编写一个没有 XSD 编译的 JAXB 类。
谁能告诉我两者的区别
@XmlSchemaType(name = "normalizedString")
private String normalized;
和
@XmlJavaTypeAdapter(NormalizedStringAdapter.class)
private String normalized;
?
【问题讨论】:
标签: jaxb
@XmlSchemaType(name = "normalizedString")
private String normalized;
当使用上述注解时,在生成 XML 模式时会为该属性对应的属性/元素指定 xsd:normalizedString 类型。
@XmlJavaTypeAdapter(NormalizedStringAdapter.class)
private String normalized;
NormalizedStringAdapter 在解组操作期间完成工作:删除换行符、回车符和制表符。
示例
input.xml
<?xml version="1.0" encoding="UTF-8"?>
<root>
<adapter> A B
C </adapter>
<schemaType> A B
C </schemaType>
<control> A B
C </control>
</root>
根
package forum383861;
import javax.xml.bind.annotation.*;
import javax.xml.bind.annotation.adapters.*;
@XmlRootElement
@XmlType(propOrder={"adapter", "schemaType", "control"})
public class Root {
private String adapter;
private String schemaType;
private String control;
@XmlJavaTypeAdapter(NormalizedStringAdapter.class)
public String getAdapter() {
return adapter;
}
public void setAdapter(String adpater) {
this.adapter = adpater;
}
@XmlSchemaType(name="normalizedString")
public String getSchemaType() {
return schemaType;
}
public void setSchemaType(String schemaType) {
this.schemaType = schemaType;
}
public String getControl() {
return control;
}
public void setControl(String control) {
this.control = control;
}
}
演示
package forum383861;
import java.io.*;
import javax.xml.bind.*;
import javax.xml.transform.Result;
import javax.xml.transform.stream.StreamResult;
public class Demo {
public static void main(String[] args) throws Exception {
JAXBContext jc = JAXBContext.newInstance(Root.class);
Unmarshaller unmarshaller = jc.createUnmarshaller();
File xml = new File("src/forum383861/input.xml");
Root root = (Root) unmarshaller.unmarshal(xml);
Marshaller marshaller = jc.createMarshaller();
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
marshaller.marshal(root, System.out);
System.out.println();
jc.generateSchema(new SchemaOutputResolver() {
@Override
public Result createOutput(String arg0, String arg1) throws IOException {
StreamResult result = new StreamResult(System.out);
result.setSystemId(arg1);
return result;
}
});
}
}
输出
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<root>
<adapter> A B C </adapter>
<schemaType> A B
C </schemaType>
<control> A B
C </control>
</root>
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<xs:schema version="1.0" xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="root" type="root"/>
<xs:complexType name="root">
<xs:sequence>
<xs:element name="adapter" type="xs:string" minOccurs="0"/>
<xs:element name="schemaType" type="xs:normalizedString" minOccurs="0"/>
<xs:element name="control" type="xs:string" minOccurs="0"/>
</xs:sequence>
</xs:complexType>
</xs:schema>
【讨论】:
@XmlSchemaType(name = "normalizedString") 用于使用 POJO 生成 XSD,@XmlJavaTypeAdapter(NormalizedStringAdapter.class) 用于 Marshal/Unmarhal,对吗?谢谢。
xs:token的适配器吗?
xs:token。我刚刚找到CollapsedStringAdapter。 :)
Collapsed|NormalizedStringAdapter 的marshal(String s) 方法与No-op. Just return the same string given as the parameter. 结合了。