gpt4 book ai didi

java - Android - 读取 XML 问题

转载 作者:行者123 更新时间:2023-12-02 07:58:59 26 4
gpt4 key购买 nike

我有一个使用 .NET Web 服务的应用程序,该服务返回 XML 数据字符串。我正在尝试读取此 XML 并将其插入到本地 SQLite 数据库中,但遇到了一些麻烦。以下是 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”,以便我可以立即插入数据?看来我需要在某个地方另一个循环,只是不太确定在哪里。

最佳答案

如果我正确理解你的问题,这就是你可能需要做的。

Node node = nodeList.item(0);

返回

 <Table> 
<IssueID>15351</IssueID>
<IssueSummary>Computer keeps crashing. This continues to be a problem</IssueSummary>
<Notes />
</Table>

node.getChildNodes().getLength();

返回 3。

  IssueInfo issue = new IssueInfo();

//遍历每个子节点并找出节点名称并填充它。

 for(int i = 0; i < node.getChildNodes().getLength(); i++){

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 逻辑移出循环。

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();
}

关于java - Android - 读取 XML 问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9199284/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com