答案 0 :(得分:1)
好的,首先你需要创建一个解析器下面是执行此操作的代码:
public static void readTemplateFile(Context context){
/**
Include File Checking
*/
try {
XML_Handler_Template myExHan = new XML_Handler_Template();
InputStreamReader isr = new
FileReader( new File(Environment.getExternalStorageDirectory().getPath() + "/Library Template.xml" ));
XML_Handler_Template.context = context;
SAXParserFactory spf = SAXParserFactory.newInstance();
SAXParser sp = spf.newSAXParser();
XMLReader xr = sp.getXMLReader();
xr.setContentHandler((ContentHandler) myExHan);
xr.parse(new InputSource(isr));
} catch (Exception e) {
Toast.makeText(context, ">" + e.getMessage(), Toast.LENGTH_LONG).show();
}
}
然后您需要一个处理程序类。在上面的例子中,我的类叫做XML_Handler_Template。
FileReader(new File(Environment.getExternalStorageDirectory()。getPath()+“/ FINILATH / FILE.XML”));
以下是XML_Handler_Class,它是空白的:
import org.xml.sax.Attributes; import org.xml.sax.SAXException; import org.xml.sax.helpers.DefaultHandler;
公共类XML_Handler_Template扩展了DefaultHandler {
public static Context context = null;
@Override
public void startDocument() throws SAXException {
//this is called when the document is first read
}
@Override
public void startElement(String namespaceURI, String localName,
String qName, Attributes atts) throws SAXException {
//This is called when a new Tag is opened
//localName holds the Tag Name, the Value is got from the
//Characters function at the end of this class
//the attributes for each tag are stored in the atts array, you can either handle the attribute values here or pass the information to a separate function to handle them,
if (atts.getLength()>0){
for (int i=0;i<atts.getLength();i++){
addAttrib(atts.getLocalName(i) , atts.getValue(i)) ;
}
}
}
@Override
public void endElement(String namespaceURI, String localName, String qName)
throws SAXException {
//This is called when a Tag is closed
}
@Override
public void endDocument() throws SAXException {
//this is called when the document is closed
}
@Override
public void characters(char ch[], int start, int length) {
//This is where the value of a Tag are read
String value = new String( ch , start , length );
// You may want to include a replaceAll("\r","") and replaceAll("\n","") to remove any hidden chars
}
}
玩这个游戏并看看你现在如何继续= 0)我将一个上下文传递给了班级,所以在我学习的时候,我可以使用toasts来向我展示正在阅读的值。