我正在编写一个没有XSD编译的JAXB类。
任何人都可以告诉我
之间的区别@XmlSchemaType(name = "normalizedString")
private String normalized;
和
@XmlJavaTypeAdapter(NormalizedStringAdapter.class)
private String normalized;
答案 0 :(得分:6)
@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>