package com.example.myapplication;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Context;
import android.os.Bundle;
import android.util.Log;
import android.util.Xml;
import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;
import org.xmlpull.v1.XmlPullParserFactory;
import org.xmlpull.v1.XmlSerializer;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
public class MainActivity extends AppCompatActivity {
private Context mContext;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
String path = null;
mContext = getApplicationContext();
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.N) {
File f = mContext.getDataDir();
path = f.getAbsolutePath();
Log.d("hanchd","ath.getAbsolutePath:"+path);
}
setContentView(R.layout.activity_main);
//prinfFile(path+"/b.xml");
try {
WriteXml(path+"/b.xml");
} catch (IOException e) {
e.printStackTrace();
}
prinfFile(path+"/b.xml");
try {
WriteXml2(path+"/b.xml");
} catch (IOException e) {
e.printStackTrace();
}
prinfFile(path+"/b.xml");
PullParseXML(path+"/b.xml");
}
public void PullParseXML(String filepath) {
XmlPullParserFactory factory = null;
try {
factory = XmlPullParserFactory.newInstance();
} catch (XmlPullParserException e) {
e.printStackTrace();
}
try {
XmlPullParser xmlPullParser = factory.newPullParser();
} catch (XmlPullParserException e) {
e.printStackTrace();
}
File file = new File(filepath);
if (!file.exists()) {
try {
file.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
file = new File(filepath);
}
FileInputStream input = null;
try {
input = new FileInputStream(file);
} catch (FileNotFoundException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
if (input == null)
return;
XmlPullParser parser = Xml.newPullParser();// 获取解析器
try {
parser.setInput(input, "UTF-8");// 设置输入流,指定码表
} catch (XmlPullParserException e) {
e.printStackTrace();
}
try {
for (int type = parser.getEventType(); type != XmlPullParser.END_DOCUMENT; type = parser.next()) {
if (type == XmlPullParser.START_TAG) {//标签开始事件 <?xml version="1.0" encoding="UTF-8"?>
if ("key".equals(parser.getName())) {//<person>标签
String res =parser.getAttributeValue(null, "key_mnt");
Log.d("hanchd","read key_mnt res:"+res);
} else if ("status".equals(parser.getName())) {//<name>标签
String res =parser.getAttributeValue(null, "state");
Log.d("hanchd","read state res:"+res);
} else if ("age".equals(parser.getName())) {//<age>标签
}
}
}
} catch (XmlPullParserException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
public void WriteXml(String filepath) throws IllegalArgumentException, IllegalStateException, IOException {
Log.d("hanch","WriteXml start");
File file = new File(filepath);
if(!file.exists()){
try {
file.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
file = new File(filepath);
}
FileOutputStream out = null;
try {
out = new FileOutputStream(file);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
if(out == null)
return;
XmlSerializer serializer = Xml.newSerializer();
serializer.setOutput(out, "UTF-8");
serializer.startDocument("UTF-8", true);
serializer.startTag("", "key");
serializer.attribute(null,"key_mnt","sdfsdfds");
serializer.endTag("", "key");
serializer.endDocument();
out.flush();
out.close();
Log.d("hanch","WriteXml end");
}
public void WriteXml2(String filepath) throws IllegalArgumentException, IllegalStateException, IOException {
Log.d("hanch","WriteXml2 start");
File file = new File(filepath);
if(!file.exists()){
try {
file.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
file = new File(filepath);
}
FileOutputStream out = null;
try {
out = new FileOutputStream(file);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
if(out == null)
return;
XmlSerializer serializer = Xml.newSerializer();
serializer.setOutput(out, "UTF-8");
serializer.startDocument("UTF-8", true);
serializer.startTag("", "status");
serializer.attribute(null,"state","on");
serializer.endTag("", "status");
serializer.endDocument();
out.flush();
out.close();
Log.d("hanch","WriteXml2 end");
}
public void prinfFile(String path) {
FileReader fr = null;
try {
fr = new FileReader(path);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
BufferedReader br=new BufferedReader(fr);
StringBuffer sb = new StringBuffer();
String str = null;
while (true) {
try {
if (!((str = br.readLine()) != null)) break;
} catch (IOException e) {
e.printStackTrace();
}
sb.append(str);
}
Log.d("hanch","xml content:"+sb.toString());
}
}
|