-1

实际上我正在研究网络服务。我已经获得了一个链接 就像http://www.url.com/requesthandler.ashx.现在我必须从此链接访问数据并将其显示在我的自定义 gridView 中,其中包含一个 textView 和一个 ImageVIew。

我以 XML 格式从给定 URL 获取响应,如下所示。

<Result><Status>OK</Status><SearchedText>in</SearchedText><Books><Book><BookID>161</BookID><BookCode>EN161</BookCode><BookTitle>A Brief History of the Fourteen Infallibles</BookTitle><BookAuthor>Anonymous</BookAuthor><DatePublished>5/18/2012 12:00:00 AM</DatePublished><CPID>23</CPID><StatusID>168</StatusID><CPName>Kazim.Homayee</CPName><CPEmail>kazim.homayee@gmail.com</CPEmail><HandCoverPrice></HandCoverPrice><PDFPrice>0.00</PDFPrice><PaperBackPrice></PaperBackPrice><EPubPrice></EPubPrice><BookImage>1.jpg</BookImage><Language>English</Language><ShippingCity>Karachi</ShippingCity><ShippingState>Sindh</ShippingState><ShippingCountry>Pakistan</ShippingCountry><BookUploadPath>EN161</BookUploadPath></Book><Book><BookID>263</BookID><BookCode>EN263</BookCode><BookTitle>A Call To Divine Unity</BookTitle><BookAuthor>Mansoor L Limba</BookAuthor><DatePublished>7/17/2003 12:00:00 AM</DatePublished><CPID>23</CPID><StatusID>287</StatusID><CPName>Kazim.Homayee</CPName><CPEmail>kazim.homayee@gmail.com</CPEmail><HandCoverPrice></HandCoverPrice><PDFPrice>0.00</PDFPrice><PaperBackPrice></PaperBackPrice><EPubPrice></EPubPrice><BookImage>1.jpg</BookImage><Language>English</Language><ShippingCity>Karachi</ShippingCity><ShippingState>Sindh</ShippingState><ShippingCountry>Pakistan</ShippingCountry><BookUploadPath>EN263</BookUploadPath></Book><Book><BookID>6</BookID><BookCode>EN6</BookCode><BookTitle>A Code of Practice for Muslims in the West</BookTitle><BookAuthor>Abdul Hadi al-Hakim</BookAuthor><DatePublished>3/16/2012 12:00:00 AM</DatePublished><CPID>23</CPID><StatusID>6</StatusID><CPName>Kazim.Homayee</CPName><CPEmail>kazim.homayee@gmail.com</CPEmail><HandCoverPrice></HandCoverPrice><PDFPrice>0.00</PDFPrice><PaperBackPrice></PaperBackPrice><EPubPrice></EPubPrice><BookImage>1.jpg</BookImage><Language>English</Language><ShippingCity>Karachi</ShippingCity><ShippingState>Sindh</ShippingState><ShippingCountry>Pakistan</ShippingCountry><BookUploadPath>EN6</BookUploadPath></Book><Book><BookID>9</BookID><BookCode>EN9</BookCode><BookTitle>A DIVINCE Perspective on Rights</BookTitle><BookAuthor>Ghodratullah Mashayekhi</BookAuthor><DatePublished>3/16/2012 12:00:00 AM</DatePublished><CPID>23</CPID><StatusID>16</StatusID><CPName>Kazim.Homayee</CPName><CPEmail>kazim.homayee@gmail.com</CPEmail><HandCoverPrice></HandCoverPrice><PDFPrice>0.00</PDFPrice><PaperBackPrice></PaperBackPrice><EPubPrice></EPubPrice><BookImage>1.jpg</BookImage><Language>English</Language><ShippingCity>Karachi</ShippingCity><ShippingState>Sindh</ShippingState><ShippingCountry>Pakistan</ShippingCountry><BookUploadPath>EN9</BookUploadPath></Book><Book><BookID>247</BookID><BookCode>EN247</BookCode><BookTitle>A Divine Perspective On Rights</BookTitle><BookAuthor>Imam Sajjad Ali ibn al-Hussein</BookAuthor><DatePublished>7/12/2008 12:00:00 AM</DatePublished><CPID>23</CPID><StatusID>950</StatusID><CPName>Kazim.Homayee</CPName><CPEmail>kazim.homayee@gmail.com</CPEmail><HandCoverPrice></HandCoverPrice><PDFPrice>0.00</PDFPrice><PaperBackPrice></PaperBackPrice><EPubPrice></EPubPrice><BookImage>1.jpg</BookImage><Language>English</Language><ShippingCity>Karachi</ShippingCity><ShippingState>Sindh</ShippingState><ShippingCountry>Pakistan</ShippingCountry><BookUploadPath>EN247</BookUploadPath></Book><Book><BookID>165</BookID><BookCode>EN165</BookCode><BookTitle>A Document of Instruction by Imam Ali (as)</BookTitle><BookAuthor>Anonymous</BookAuthor><DatePublished>5/18/2012 12:00:00 AM</DatePublished><CPID>23</CPID><StatusID>185</StatusID><CPName>Kazim.Homayee</CPName><CPEmail>kazim.homayee@gmail.com</CPEmail><HandCoverPrice></HandCoverPrice><PDFPrice>0.00</PDFPrice><PaperBackPrice></PaperBackPrice><EPubPrice></EPubPrice><BookImage>1.jpg</BookImage><Language>English</Language><ShippingCity>Karachi</ShippingCity><ShippingState>Sindh</ShippingState><ShippingCountry>Pakistan</ShippingCountry><BookUploadPath>EN165</BookUploadPath></Book><Book><BookID>12</BookID><BookCode>EN12</BookCode><BookTitle>A Probe into the History of Ashura</BookTitle><BookAuthor>Dr Ibrahim Ayati</B
  • 这个 XML 数据包含大量数据,但我只需要访问<BookTitle>,<BookCode><BookImage>标记内的数据并将其显示在 textVIew 和 imageVIew 中。
  • 我还需要一些关于 android 中可用的不同类型解析器的信息。

提前致谢。

4

1 回答 1

1

通常有两种解析 XML 的方法:

  1. 一次在文档中流式传输一个标签,并且只处理您感兴趣的标签
  2. 一次将整个文档加载到分层树中,然后查找您感兴趣的节点

方法 1 的内存效率要高得多,Android 开发人员文档在此处建议的也是如此。

方法 2 通常更容易上手。但由于它效率较低,因此不是首选方法,因此在 Android 开发人员文档中隐藏得更多。请参阅 DocumentBuilderFactory 上的此API 文档本教程以开始使用。

显示症结的片段:

public Document parseXml(String xml) throws Exception {
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    DocumentBuilder building = factory.newDocumentBuilder();
    InputSource input = new InputSource();
    input.setCharacterStream(new StringReader(xml));
    return builder.parse(input); 
}
于 2012-11-13T15:11:40.557 回答