我想写入SD卡中的XML文件。我发现了这段代码,我不知道如何使用InputStream。
import java.io.IOException;
import java.io.StringReader;
import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException.html;
import org.xmlpull.v1.XmlPullParserFactory;
public class SimpleXmlPullApp
{
public static void main (String args[])
throws XmlPullParserException, IOException
{
XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
factory.setNamespaceAware(true);
XmlPullParser xpp = factory.newPullParser();
xpp.setInput(new StringReader ("<foo>Hello World!</foo>"));
int eventType = xpp.getEventType();
while (eventType != XmlPullParser.END_DOCUMENT) {
if(eventType == XmlPullParser.START_DOCUMENT) {
System.out.println("Start document");
} else if(eventType == XmlPullParser.END_DOCUMENT) {
System.out.println("End document");
} else if(eventType == XmlPullParser.START_TAG) {
System.out.println("Start tag "+xpp.getName());
} else if(eventType == XmlPullParser.END_TAG) {
System.out.println("End tag "+xpp.getName());
} else if(eventType == XmlPullParser.TEXT) {
System.out.println("Text "+xpp.getText());
}
eventType = xpp.next();
}
}
}
这是我的xml文件的路径:
Environment.getExternalStorageDirectory() + "/DataLog.xml"
答案 0 :(得分:0)
您是否尝试过此http://developer.android.com/training/basics/network-ops/xml.html
它是如何使用xml pull解析器的分步指南,它是我以前学习使用它的。
要创建输入流,请使用
String path = Environment.getExternalStorageDirectory() + "/DataLog.xml";
FileInputStream fis = new FileInputStream(path);