【鸿蒙】鸿蒙如何进行数据解析
【问题描述】有时候我们从服务器获取是 xml 格式数据,我们需要将 xml 转化成 model 对象,该如何使用呢?下面举个例子说明一下,将分以下几步进行
1.准备条件 创建xml文件,创建model对象,构建界面
2.数据进行解析操作(重点)
3.运行效果
第一步准备条件 创建 xml 文件,创建 model 对象,构建界面
1.1 在 rawfile 新建 xml 文件,代码如下
<?xml version="1.0" encoding="utf-8"?>
<note>
<to>George</to>
<from>John</from>
<heading>Reminder</heading>
<body>Don't forget the meeting!</body>
</note>
1.2 新建 class model 代码如下
package com.harmony.alliance.mydemo.model;
public class note {
private String from;
private String heading;
private String body;
private String to;
public String getFrom() {
return from;
}
public void setFrom(String from) {
this.from = from;
}
public String getBody() {
return body;
}
public void setBody(String body) {
this.body = body;
}
public String getTo() {
return to;
}
public void setTo(String to) {
this.to = to;
}
public String getHeading() {
return heading;
}
public void setHeading(String heading) {
this.heading = heading;
}
@Override
public String toString() {
return "note{" +
"from='" + from + '\'' +
", heading='" + heading + '\'' +
", body='" + body + '\'' +
<