你可以嘗試這樣的事情:
// Get your file from internet
URL url = new URL("http://url.com/info.xsl");
URLConnection connection = url.openConnection();
InputStream in = connection.getInputStream();
String filePath = Environment.getExternalStorageDirectory().getAbsolutePath()+ "/filename.xml"
File file = new File (filePath);
CreateFileFromInputStream(in, filePath) ;
// Parse it with document builder factory
DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder dBuilder = null;
dBuilder = dbFactory.newDocumentBuilder();
Document doc = null;
doc = dBuilder.parse(file);
doc.getDocumentElement().normalize();
// The root element is
doc.getDocumentElement().getNodeName();
NodeList nList = doc.getElementsByTagName("Name");
for (int i = 0 ; i < nList.getLength() ; i++) {
Element element = (Element) nList.item(i) ;
String name = getCharacterDataFromElement(element);
}
有:
public static String getCharacterDataFromElement(Element e)
{
Node node = e.getFirstChild();
if (node instanceof CharacterData)
{
CharacterData cd = (CharacterData) node;
return cd.getData();
}
return "";
}
和:
public void CreateFileFromInputStream(InputStream inStream, String path) throws IOException {
// write the inputStream to a FileOutputStream
OutputStream out = new FileOutputStream(new File(path));
int read = 0;
byte[] bytes = new byte[1024];
while ((read = inStream.read(bytes)) != -1) {
out.write(bytes, 0, read);
}
inStream.close();
out.flush();
out.close();
}
我希望它可以幫助...
ü意味着ü希望讀取特定節點並在textview中顯示??? –
您可以使用xml解析 – Piyush
我想讀取標籤之間的一行並顯示它 – user3064772