1

我想在下面的 XML 代码中显示 maxLength 属性。我需要在我的 java 类中做什么。

JAVA Class need to modified occording the below xml:

public class xxxx{
 protected String directionsToSite;
}

===========================================================
XML Expected will be like this:

  <element name="accessToAntennas" minOccurs="0">
            <simpleType>
             <restriction base="{http://www.w3.org/2001/XMLSchema}string">
               <maxLength value="500"/>
            <restriction>
           <simpleType>
         </element>
4

1 回答 1

0

尝试使用注释@Size(min=..., max=...) 或者您可以尝试:

public class TheClazz {
  @XmlJavaTypeAdapter(value = MyXmlAdapter.class, type = String.class)
  private String myString;
}

public class MyXmlAdapter extends XmlAdapter<String, String> {

    private final int MAX = 500; 

    @Override
    public String unmarshal(String s) throws Exception {
        return s.size() > MAX ? throw new Exception() : s; 
    }

    @Override
    public String marshal(String s) throws Exception {
        return s.substring(0, MAX);
    }
}
于 2016-02-03T13:47:52.830 回答