【问题标题】:JAXB difference between @XmlSchemaType(name="normalizedString") and @XmlJavaTypeAdapter(NormalizedStringAdapter.class)@XmlSchemaType(name="normalizedString") 和 @XmlJavaTypeAdapter(NormalizedStringAdapter.class) 之间的 JAXB 区别
【发布时间】:2012-03-08 07:20:58
【问题描述】:

我正在编写一个没有 XSD 编译的 JAXB 类。

谁能告诉我两者的区别

@XmlSchemaType(name = "normalizedString")
private String normalized;

@XmlJavaTypeAdapter(NormalizedStringAdapter.class)
private String normalized;

?

【问题讨论】:

    标签: jaxb


    【解决方案1】:
    @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|NormalizedStringAdaptermarshal(String s) 方法与No-op. Just return the same string given as the parameter. 结合了。
    猜你喜欢
    • 2014-10-28
    • 1970-01-01
    • 2019-03-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-01-31
    • 1970-01-01
    相关资源
    最近更新 更多