我有一個應用程序,它使用.NET Web服務,它返回一個XML數據字符串。我試圖讀取這個XML並將其插入本地SQLite數據庫,但我遇到了一些麻煩。下面是XML的一個樣本:Android - 讀取XML問題
<?xml version="1.0" encoding="utf-8" ?>
<string xmlns="RemoteWebService"><OpenIssues> <Table> <IssueID>15351</IssueID> <IssueSummary>Computer keeps crashing. This continues to be a problem</IssueSummary> <LocationName>West Side</LocationName> <Status>WIP</Status> <CustomerID>89755</CustomerID> <CustomerName>West Side Computers</CustomerName> <CustomerShortName>WSC</CustomerShortName> <Notes /> <STATUS1>Work In Progress</STATUS1> <SubmittedBy>WSC - Tom Johns</SubmittedBy> <EQ_Replaced>true</EQ_Replaced></Table> </OpenIssues></string>
使用DOM,我試圖解析結果像這樣:
private void GetLatestData(String response) throws ParserConfigurationException, SAXException, IOException{
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
Document doc = db.parse(new InputSource(new StringReader(response)));
//Normalize the document.
doc.getDocumentElement().normalize();
//Get Root Node.
NodeList nodeList = doc.getElementsByTagName("Table");
Node node = nodeList.item(0);
//Get Child Nodes.
for(int i = 0; i < node.getChildNodes().getLength(); i++){
IssueInfo issue = new IssueInfo();
Node tempNode = node.getChildNodes().item(i);
if(tempNode.getNodeName().equalsIgnoreCase("IssueID")){
issue.setIssueNumber(Long.parseLong(tempNode.getTextContent()));
}
if(tempNode.getNodeName().equalsIgnoreCase("IssueSummary")){
issue.setIssueNumber(Long.parseLong(tempNode.getTextContent()));
}
if(issue.getIssueNumber() > 0 && issue.getIssueSummary() != null){
creator = new IssueInfoCreator(this, DBVersion);
creator.open();
creator.InsertIssue(issue.getIssueNumber(), DateFormat.getDateInstance().format(new Date()), issue.getIssueSummary());
creator.close();
}
}
}
當我運行它通過調試器,它就會「IssueID」就好了但是我怎麼才能在它之後找到下一個節點「IssueSummary」,這樣我就可以一次插入數據?似乎我需要另一個循環,只是不太確定。
是否特定於Android?也就是說,在IDE中運行代碼時是否會出現與原始Java相同的錯誤?乍看之下,您似乎沒有理由不能像抓取'IssueID'一樣訪問'IssueSummary'。 – 2012-02-08 18:37:49
我對Android開發很新,但是我正在用Eclipse開發這個應用程序,當我調試它時,它就像一個Android應用程序。不知道如何將它作爲Java應用程序運行。 – Robert 2012-02-08 18:41:25
這是DOM解析不考慮Java/Android。 – kosa 2012-02-08 18:50:37