Summary of basic operations on types in java

1. Convert json string to object

SyncCarriageStatusDTO dto= JSON.parseObject(value,SyncCarriageStatusDTO.class);

2. Convert the enumeration string to the enumeration type

RefTypeEnum refType = RefTypeEnum.valueof (dto.getRefType ())

3. Object is not empty judgment

CollectionUtils.isNotEmpty(carriageRefs)

4. XML string conversion object or XML operation

   4.1 Use JAXBContext

       JAXBContext jaxbContext = JAXBContext.newInstance(cls);
       Unmarshaller um = jaxbContext.createUnmarshaller();
       t = (T)um.unmarshal(new StringReader(xmlStr));

  4.2 Using XStream

   if(! xmap.containsKey(cls.getName())){

    XStream xstream = new XStream(new DomDriver());
   xstream.processAnnotations(cls);
   xstream.ignoreUnknownElements();
   xmap.put(cls.getName(), xstream);
    }

    T t = (T) xmap.get(cls.getName()).fromXML(xmlStr);

  4.3 Using DocumentHelper.parseText(xml)

Node eventType = document.selectSingleNode("//logisticsEvent/eventHeader/eventType");
if (eventType != null && StringUtils.isNotBlank(eventType.getText())) {
type = CainiaoMessageEventTypeEnum.valueOf(eventType.getText().trim());
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325058649&siteId=291194637