共计 5978 个字符,预计需要花费 15 分钟才能阅读完成。
【鸿蒙】鸿蒙如何进行数据解析
【问题形容】有时候咱们从服务器获取是 xml 格局数据,咱们须要将 xml 转化成 model 对象,该如何应用呢?上面举个例子阐明一下,将分以下几步进行
- 筹备条件 创立 xml 文件,创立 model 对象,构建界面
- 数据进行解析操作(重点)
- 运行成果
第一步筹备条件 创立 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 + '\'' +
", to='" + to + '\'' +
'}';
}
}
1.3 新建数据的 abilityslice,xml 如下
<?xml version="1.0" encoding="utf-8"?>
<DirectionalLayout
xmlns:ohos="http://schemas.huawei.com/res/ohos"
ohos:height="match_parent"
ohos:width="match_parent"
ohos:orientation="vertical">
<Text
ohos:id="$+id:mStartParse"
ohos:height="100vp"
ohos:width="match_parent"
ohos:text="开始解析"
ohos:text_size="20fp"
ohos:text_color="#ffffff"
ohos:text_alignment="center"
ohos:background_element="#ed6262"/>
<Text
ohos:id="$+id:mTo"
ohos:height="100fp"
ohos:width="match_parent"
ohos:text_size="20fp"
ohos:text_color="#ed6262"
ohos:background_element="#ffffff"/>
<Text
ohos:id="$+id:mfrom"
ohos:height="100fp"
ohos:width="match_parent"
ohos:text_size="20fp"
ohos:text_color="#ed6262"
ohos:background_element="#ffffff"/>
<Text
ohos:id="$+id:mheading"
ohos:height="100fp"
ohos:width="match_parent"
ohos:text_size="20fp"
ohos:text_color="#ed6262"
ohos:background_element="#ffffff"/>
<Text
ohos:id="$+id:mbody"
ohos:height="100fp"
ohos:width="match_parent"
ohos:text_size="20fp"
ohos:text_color="#ed6262"
ohos:background_element="#ffffff"/>
</DirectionalLayout>
界面成果如下
第二步数据解析
2.1 参考资料如下
SAXParser
https://developer.harmonyos.c…
XMLReader
https://developer.harmonyos.c…
该性能和 Android 的 demo 类似能够参考 Android 的 demo 链接
https://blog.csdn.net/bzlj291…
在上述四个接口中,最重要的就是 ContentHandler 这个接口,上面是对这个接口办法的阐明:
// 设置一个能够定位文档内容事件产生地位的定位器对象
public void setDocumentLocator(Locator locator)
// 用于解决文档解析开始事件
public void startDocument()throws SAXException
// 解决元素开始事件,从参数中能够取得元素所在名称空间的 uri,元素名称,属性类表等信息
public void startElement(String namespacesURI , String localName , String qName , Attributes atts) throws SAXException
// 解决元素完结事件,从参数中能够取得元素所在名称空间的 uri,元素名称等信息
public void endElement(String namespacesURI , String localName , String qName) throws SAXException
// 解决元素的字符内容,从参数中能够取得内容
public void characters(char[] ch , int start , int length) throws SAXException
新建 SaxHelper 代码如下
package com.harmony.alliance.mydemo.model;
import ohos.org.xml.sax.Attributes;
import ohos.org.xml.sax.SAXException;
import ohos.org.xml.sax.helpers.DefaultHandler;
/**
* Created by Jay on 2015/9/8 0008.
*/
public class SaxHelper extends DefaultHandler {
// 以后解析的元素标签
private String tagName = null;
private note mNote;
/**
* 当读取到文档开始标记是触发,通常在这里实现一些初始化操作
*/
@Override
public void startDocument() throws SAXException {mNote=new note();
}
/**
* 读到一个开始标签时调用, 第二个参数为标签名, 最初一个参数为属性数组
*/
@Override
public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {System.err.println("####===>>"+localName);
this.tagName = localName;
}
/**
* 读到到内容, 第一个参数为字符串内容, 前面顺次为起始地位与长度
*/
@Override
public void characters(char[] ch, int start, int length) throws SAXException {
// 判断以后标签是否无效
if (this.tagName != null) {String data = new String(ch, start, length);
// 读取标签中的内容
if (this.tagName.equals("to")) {mNote.setTo(data);
} else if (this.tagName.equals("from")) {mNote.setFrom(data);
}else if(this.tagName.equals("heading")){mNote.setHeading(data);
}else if(this.tagName.equals("body")){mNote.setBody(data);
}
}
}
/**
* 解决元素完结时触发, 这里将对象增加到联合中
*/
@Override
public void endElement(String uri, String localName, String qName) throws SAXException {this.tagName = null;}
/**
* 读取到文档结尾时触发,*/
@Override
public void endDocument() throws SAXException {super.endDocument();
System.err.println("#####===>>"+mNote.toString());
// Log.i("SAX", "读取到文档尾,xml 解析完结");
}
public note getmNote() {return mNote;}
}
AbilitySlice 代码如下
package com.harmony.alliance.mydemo.slice;
import com.harmony.alliance.mydemo.ResourceTable;
import com.harmony.alliance.mydemo.model.SaxHelper;
import com.harmony.alliance.mydemo.model.note;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.agp.components.Component;
import ohos.agp.components.Text;
import ohos.javax.xml.parsers.SAXParser;
import ohos.javax.xml.parsers.SAXParserFactory;
import ohos.org.xml.sax.InputSource;
import ohos.org.xml.sax.XMLReader;
import java.io.InputStream;
import java.util.Locale;
public class myXmlParseAbilitySlice extends AbilitySlice {
private Text mStartParse,mTo,mfrom,mheading,mbody;
@Override
protected void onStart(Intent intent) {super.onStart(intent);
setUIContent(ResourceTable.Layout_my_xml_parse);
mStartParse= (Text) findComponentById(ResourceTable.Id_mStartParse);
mTo= (Text) findComponentById(ResourceTable.Id_mTo);
mfrom= (Text) findComponentById(ResourceTable.Id_mfrom);
mheading= (Text) findComponentById(ResourceTable.Id_mheading);
mbody= (Text) findComponentById(ResourceTable.Id_mbody);
mStartParse.setClickedListener(new Component.ClickedListener() {
@Override
public void onClick(Component component) {
try {
String rawFilePath = "note.xml";
String filePath = String.format(Locale.ROOT, "assets/entry/resources/rawfile/%s", rawFilePath);
InputStream is = this.getClass().getClassLoader().getResourceAsStream(filePath);
InputSource is2=new InputSource(is);
SaxHelper ss = new SaxHelper();
SAXParserFactory factory = SAXParserFactory.newInstance();
factory.setNamespaceAware(true);
SAXParser parser = factory.newSAXParser();
XMLReader xmlReader=parser.getXMLReader();
xmlReader.setContentHandler(ss);
xmlReader.parse(is2);
note mNote= ss.getmNote();
mTo.setText("to:"+mNote.getTo());
mfrom.setText("From:"+mNote.getFrom());
mheading.setText("heading:"+mNote.getHeading());
mbody.setText("body:"+mNote.getBody());
is.close();}catch (Exception e){e.printStackTrace();
}
}
});
}
}
第三步运行成果如下
更多精彩内容,请见华为开发者官方论坛→https://developer.huawei.com/…