一、在Android应用中的XML文件来源
1、本地xml文件
本地XML文件可以放在应用根目录assets文件夹、res/xml、res/raw、SDcard卡、应用的data目录等;
除res/xml可直接通过getXml(int id)获取XML文档,返回一个解析器对象(XmlResourceParer:XmlResourceParer是XmlPullParser的子类),其它位置情况都可以获取XML文档,返回一个Inputstream对象,进行读取数据,获取方法分别如下:
a在res/xml目录下(推荐使用):
[java] view plaincopy
XmlResourceParser xmlParser = thisgetResources()getXml(RxmlXXX);
b在res/xml、res/raw目录下:
[java] view plaincopy
InputStream inputStream = thisgetResources()openRawResource(RxmlXXX);
c在assets文件夹下(本人测试发现通过此方法获取的XML文档不能带有首行:<xml version="10" encoding="utf-8">,否则解析报错,具体原因未查明,知道原因请回复交流):
[java] view plaincopy
InputStream inputStream = getResources()getAssets()open(fileName);
d在应用指定目录下(SDcard,应用data目录等):
[java] view plaincopy
// path路径根据实际项目修改,此次获取SDcard根目录
String path = EnvironmentgetExternalStorageDirectory()toString();
File xmlFlie = new File(path+fileName);
InputStream inputStream = new FileInputStream(xmlFlie);
2、通过url得到的xml文件
很多时候需要解析xml文件都用于客户端与服务器之间的数据交互,比如解析google天气预报信息,或自己项目内定的一些XML数据结构,其中通过URL,使用Default>
下面是我写的生成XML文件的例子,主要是用递归来完成嵌套的XML生成。
生成的格式为:
<Datas>
<Data>
<year>
</year>
<paper>
<name> </name>
<number></number>
<weight></weight>
<paper>
</paper>
</paper>
</Data>
</Datas>
#include <fstream>
#include <iostream>
#include <string>
#include <ctime>
#include <cstdlib>
using namespace std;
const int MAX_YEAR = 6;
const int MAX_NODENAME = 8;
const int MAX_DEPTH = 1;
const int MAX_ITEM = 15;
const int MAX_TAGS = 50;
enum NodeName
{
DATAS,
DATA,
YEAR,
PAPERS,
NUMBER,
WEIGHT,
TAG,
NAME
};
string NodeBegin[MAX_NODENAME] = {"<Datas>", "<Data>", "<year>", "<paper>", "<number>", "<weight>", "<tag>", "<name>"};
string NodeEnd[MAX_NODENAME] = {"</Datas>", "</Data>", "</year>", "</paper>", "</number>", "</weight>", "</tag>", "</name>"};
string Years[MAX_YEAR] = {"2000","2001","2002","2003","2004","2005"};
//_T的意思是通知编译器,自行进行字符串的多字节/Unicode转换。
//而L表示,该字符串为Unicode版本。
ofstream outfile(L"首页xml");
int Random(int i)
{
return (rand()%i+1);
}
void Recursive(int level, string parentName, string year)
{
if (level == 1)
{
for (int i = 0; i < MAX_ITEM; i++)
{
outfile<<NodeBegin[PAPERS]<<endl;
outfile<<NodeBegin[NAME]<<parentName<<"-"<<i<<NodeEnd[NAME]<<endl;
outfile<<NodeBegin[NUMBER]<<Random(10)<<NodeEnd[NUMBER]<<endl;
outfile<<NodeBegin[WEIGHT]<<Random(10)<<NodeEnd[WEIGHT]<<endl;
outfile<<NodeEnd[PAPERS]<<endl;
}
for (int j =0; j < MAX_TAGS; j++)
{
outfile<<NodeBegin[TAG]<<endl;
outfile<<NodeBegin[NAME]<<year<<":"<<parentName<<"-"<<j<<NodeEnd[NAME]<<endl;
outfile<<NodeBegin[WEIGHT]<<Random(10)<<NodeEnd[WEIGHT]<<endl;
outfile<<NodeEnd[TAG]<<endl;
}
}
else
{
for (int i = 0; i < MAX_ITEM; i++)
{
char a[10];
string str;
itoa(i, a, 10);
str = a;
string curName = parentName+"-"+str;
outfile<<NodeBegin[PAPERS]<<endl;
outfile<<NodeBegin[NAME]<<curName<<NodeEnd[NAME]<<endl;
outfile<<NodeBegin[NUMBER]<<Random(10)<<NodeEnd[NUMBER]<<endl;
outfile<<NodeBegin[WEIGHT]<<Random(10)<<NodeEnd[WEIGHT]<<endl;
Recursive(level-1, curName, year);
outfile<<NodeEnd[PAPERS]<<endl;
}
for (int j =0; j < MAX_TAGS; j++)
{
outfile<<NodeBegin[TAG]<<endl;
outfile<<NodeBegin[NAME]<<"Tag"<<year<<":"<<parentName<<"-"<<j<<NodeEnd[NAME]<<endl;
outfile<<NodeBegin[WEIGHT]<<Random(10)<<NodeEnd[WEIGHT]<<endl;
outfile<<NodeEnd[TAG]<<endl;
}
}
}
void GenerateXML()
{
outfile<<NodeBegin[DATAS]<<endl;
for (int i = 0; i < MAX_YEAR; i++ )
{
outfile<<NodeBegin[DATA]<<endl;
outfile<<NodeBegin[YEAR]<<Years[i]<<NodeEnd[YEAR]<<endl;
Recursive(MAX_DEPTH, "学科主题",Years[i]);
outfile<<NodeEnd[DATA]<<endl;
}
outfile<<NodeEnd[DATAS]<<endl;
}
void main()
{
locale::global(locale(""));
srand(time(NULL));
if (!outfile)
{
cout<<"can't open output file"<<endl;
}
GenerateXML();
cout<<"Generate End"<<endl;
}
如果获取到节点话,就调用attributeValue(String name)方法获取里面的值就好了。
$cat testsh
#!/bin/bash
if [ -z $1 ];then
echo 'USAGE:COMMAND FILENAME'
exit 0
fi
filename=recordtxt
HOST=(`sed -n 's/>\(\)<\/host>/\1/p' $1`)
OIDG=(`sed -n 's/>\(\)<\/oidgroupname>/\1/p' $1`)
COMM=(`sed -n 's/>\(\)<\/communitystring>/\1/p' $1`)
DESC=(`sed -n 's/>\(\)<\/description>/\1/p' $1`)
FILE=(`ls -l $filename >/dev/null 2>&1 | awk '{print $8}'`)
if [ ! -z $FILE ];then
echo -e "host\t\toidgroupname\t\tcomm\t\tdesc" >$filename
fi
for((i=0;i<${#HOST[@]};i++));do
echo -e "${HOST[i]}\t${OIDG[i]}\t${COMM[i]}\t\t${DESC[i]}" >>$filename
done
$/testsh file
$cat recordtxt
host oidgroupname comm desc
19216811 CpuUtilization_MF public 19216811_CPUUtilizaton
19216812 CpuUtilization_MF public 19216812_CPUUtilizaton
19216813 CpuUtilization_MF public 19216813_CPUUtilizaton
19216814 CpuUtilization_MF public 19216814_CPUUtilizaton
19216815 CpuUtilization_MF public 19216815_CPUUtilizaton
以上就是关于新手使用android的XML(DOM)解析问题,指定路径XML如何读取全部的内容,包括:新手使用android的XML(DOM)解析问题,指定路径XML如何读取、flash AS3.0 提取XML 的内容文字,去掉前面的标签!怎么实现!、求 C++读写取XML 文件方法!网上复制的让开等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)