@XmlSchemaType(name=";normalizedString";)和@XmlJavaTypeAdapter(NormalizedStringAdapter.class)之间的JAXB差异

@XmlSchemaType(name=";normalizedString";)和@XmlJavaTypeAdapter(NormalizedStringAdapter.class)之间的JAXB差异,jaxb,Jaxb,我正在编写一个没有XSD编译的JAXB类 有谁能告诉我两者的区别吗 @XmlSchemaType(name=“normalizedString”) 私有字符串规范化; 及 @XmlJavaTypeAdapter(NormalizedStringAdapter.class) 私有字符串规范化; ? 使用上述注释时,在生成XML模式时,将为与此属性对应的属性/元素指定xsd:normalizedString类型 @XmlJavaTypeAdapter(NormalizedStringAdapte

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

有谁能告诉我两者的区别吗

@XmlSchemaType(name=“normalizedString”)
私有字符串规范化;

@XmlJavaTypeAdapter(NormalizedStringAdapter.class)
私有字符串规范化;
?

使用上述注释时,在生成XML模式时,将为与此属性对应的属性/元素指定xsd:normalizedString类型

@XmlJavaTypeAdapter(NormalizedStringAdapter.class)
private String normalized;
NormalizedStringAdapter
在解组操作期间执行工作:删除换行符、回车符和制表符

示例

input.xml

演示

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;
            }

        });
    }

}
输出

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;
    }

}
<?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>

A、B、C
A B
C
A B
C

So
@XmlSchemaType(name=“normalizedString”)
用于使用POJO生成XSD,而
@XmlJavaTypeAdapter(NormalizedStringAdapter.class)
用于封送/取消封送,对吗?谢谢。有用于
xs:token
的适配器吗?别管
xs:token
。我刚找到。:)哎呀,我刚刚发现
marshal(String s)
方法的
折叠| NormalizedStringAdapter
apidoced带有
No-op。只需返回与参数相同的字符串。
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>