XML字符串转换为Json字符串和XML字符串转换为Java对象

张高澹
2023-12-01

记录:384

场景:把XML字符串转换为Json字符串。把XML字符串转换为Java对象。

版本:JDK 1.8

1.XML转换为JSON字符串

1.1引用Jar

Jar包:hutool-all-5.8.12.jar

类位置:cn.hutool.json.XML

1.2自定义封装方法

public static String parseXml2JSONString(String xmlStr){
  JSONObject jsonObject = XML.toJSONObject(xmlStr);
  return jsonObject.toString();
}

2.XML字符串转换为Java对象

2.1引用Jar

Jar包:rt.jar,jdk 1.8自带包。

类位置:

javax.xml.bind.JAXBContext

javax.xml.bind.Unmarshaller

2.2自定义封装方法

public static <T> T parseXml2Object(String xmlStr,Class<T> targetClass){
  JAXBContext jaxbContext = null;
  try {
    jaxbContext = JAXBContext.newInstance(targetClass);
    Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
    return (T) unmarshaller.unmarshal(new StringReader(xmlStr));
  } catch (JAXBException e) {
    e.printStackTrace();
  }
  return null;
}

3.测试main方法

3.1.main方法

在main方法做测试验证。

public static void main(String[] args) throws JAXBException {
  String xml01 = getXml01();
  String xml02 = getXml02();
  //1.测试xml字符串转换为JSON字符串
  log.info("xml01转换为Json字符串:");
  log.info(parseXml2JSONString(xml01).toString());
  log.info("xml02转换为Json字符串:");
  log.info(parseXml2JSONString(xml02).toString());
  //2.测试xml字符串转换为Java对象
  log.info("xml01转换为Java对象:");
  DataDTO dataDTO =parseXml2Object(xml01,DataDTO.class);
  log.info(dataDTO.toString());
  log.info("xml02转换为Java对象:");
  DataListDTO dataListDTO =parseXml2Object(xml01,DataListDTO.class);
  log.info(dataListDTO.toString());
}

3.2.XML字符串

(1)字符串一

<province>
  <provinceId>1</provinceId>
  <provinceName>浙江省</provinceName>
  <landArea>105500</landArea>
  <population>6577</population>
  <gross>7.77</gross>
  <city>
    <cityId>1</cityId>
    <cityName>杭州</cityName>
    <landArea>16850</landArea>
    <population>1237</population>
    <gross>1.88</gross>
    <cityDescribe>杭州是一个好城市</cityDescribe>
    <dataYear>2022</dataYear>
      <updateTime>2023-03-12 15:58:59</updateTime>
  </city>
</province>

(2)字符串二

<province>
  <provinceId>1</provinceId>
  <provinceName>浙江省</provinceName>
  <landArea>105500</landArea>
  <population>6577</population>
  <gross>7.77</gross>
  <city>
     <cityId>1</cityId>
     <cityName>杭州</cityName>
     <landArea>16850</landArea>
     <population>1237</population>
     <gross>1.81</gross>
     <cityDescribe>杭州是一个好城市</cityDescribe>
     <dataYear>2021</dataYear>
     <updateTime>2023-03-12 15:58:59</updateTime>
  </city>
  <city>
    <cityId>2</cityId>
    <cityName>杭州</cityName>
    <landArea>16850</landArea>
    <population>1237</population>
    <gross>1.88</gross>
    <cityDescribe>杭州是一个好城市</cityDescribe>
    <dataYear>2022</dataYear>
    <updateTime>2023-03-12 15:58:59</updateTime>
  </city>
</province>

4.支撑对象和数据

4.1获取数据方法getXml01()

public static String getXml01(){
 return "<province>\n" +
    "    <provinceId>1</provinceId>\n" +
    "    <provinceName>浙江省</provinceName>\n" +
    "    <landArea>105500</landArea>\n" +
    "    <population>6577</population>\n" +
    "    <gross>7.77</gross>\n" +
    "    <city>\n" +
    "        <cityId>1</cityId>\n" +
    "        <cityName>杭州</cityName>\n" +
    "        <landArea>16850</landArea>\n" +
    "        <population>1237</population>\n" +
    "        <gross>1.88</gross>\n" +
    "        <cityDescribe>杭州是一个好城市</cityDescribe>\n" +
    "        <dataYear>2022</dataYear>\n" +
    "\t\t<updateTime>2023-03-12 15:58:59</updateTime>\n" +
    "    </city>\n" +
    "</province>";
}

4.2获取数据方法getXml02()

public static String getXml02(){
 return "<province>\n" +
     "    <provinceId>1</provinceId>\n" +
     "    <provinceName>浙江省</provinceName>\n" +
     "    <landArea>105500</landArea>\n" +
     "\t<population>6577</population>\n" +
     "    <gross>7.77</gross>\n" +
     "    <city>\n" +
     "        <cityId>1</cityId>\n" +
     "        <cityName>杭州</cityName>\n" +
     "        <landArea>16850</landArea>\n" +
     "        <population>1237</population>\n" +
     "        <gross>1.81</gross>\n" +
     "        <cityDescribe>杭州是一个好城市</cityDescribe>\n" +
     "        <dataYear>2021</dataYear>\n" +
     "\t\t<updateTime>2023-03-12 15:58:59</updateTime>\n" +
     "    </city>\n" +
     "\t<city>\n" +
     "        <cityId>2</cityId>\n" +
     "        <cityName>杭州</cityName>\n" +
     "        <landArea>16850</landArea>\n" +
     "        <population>1237</population>\n" +
     "        <gross>1.88</gross>\n" +
     "        <cityDescribe>杭州是一个好城市</cityDescribe>\n" +
     "        <dataYear>2022</dataYear>\n" +
     "\t\t<updateTime>2023-03-12 15:58:59</updateTime>\n" +
     "    </city>\n" +
     "</province>";
}

4.3 DataListDTO对象

@Data
@XmlAccessorType(XmlAccessType.FIELD)
@XmlRootElement(name = "province")
@XmlType( propOrder ={"provinceId","provinceName","landArea","population","gross","city"})
public class DataListDTO implements Serializable {
  private Long provinceId;
  private String provinceName;
  private Double landArea;
  private Long population;
  private Double gross;
  private List<CityDTO> city;
}

4.4DataDTO对象

@Data
@XmlAccessorType(XmlAccessType.FIELD)
@XmlRootElement(name = "province")
@XmlType( propOrder ={"provinceId","provinceName","landArea","population","gross","city"})
public class DataDTO implements Serializable {
  private Long provinceId;
  private String provinceName;
  private Double landArea;
  private Long population;
  private Double gross;
  private CityDTO city;
}

4.5CityDTO对象

@Data
public class CityDTO implements Serializable {
  private Long cityId;
  private String cityName;
  private Double landArea;
  private Long population;
  private Double gross;
  private String cityDescribe;
  private String dataYear;
  private String updateTime;
}

以上,感谢。

2023年3月12日

 类似资料: