解析器 2.1 和 2.2

发布于 2024-10-10 06:13:16 字数 1368 浏览 0 评论 0原文

我使用以下代码使用 getElementsByTagName 检索 XML 元素文本,

此代码在 2.2 中成功,在 2.1 中失败,

有什么想法吗?

URL metafeedUrl = new URL("http://x..../Y.xml")
URLConnection connection ;     
connection= metafeedUrl.openConnection();

HttpURLConnection  httpConnection = (HttpURLConnection)connection ;        
int resposnseCode= httpConnection.getResponseCode() ;

if (resposnseCode == HttpURLConnection.HTTP_OK) {

InputStream in = httpConnection.getInputStream();

DocumentBuilderFactory dbf ;
dbf = DocumentBuilderFactory.newInstance();

DocumentBuilder db = dbf.newDocumentBuilder();


// Parse the Earthquakes entry 
Document dom = db.parse(in);
Element docEle = dom.getDocumentElement();

//ArrayList<Album> Albums = new ArrayList<Album>();


/* Returns a NodeList of all descendant Elements with a given tag name, in document order.*/

NodeList nl = docEle.getElementsByTagName("entry");

if (nl!=null && nl.getLength() > 0) {        
   for (int i = 0; i < nl.getLength(); i++) {

Element entry = (Element)nl.item(i);

/* Now on every property in Entry **/

Element title =(Element)entry.getElementsByTagName("title").item(0);          

*Here i Get an Error*
String album_Title = title.getTextContent();

Element id =(Element)entry.getElementsByTagName("id").item(0);         
String album_id = id.getTextContent(); // 

i using the follwing Code to retrive XML element text using getElementsByTagName

this code success in 2.2 and Failed in 2.1

any idea ?

URL metafeedUrl = new URL("http://x..../Y.xml")
URLConnection connection ;     
connection= metafeedUrl.openConnection();

HttpURLConnection  httpConnection = (HttpURLConnection)connection ;        
int resposnseCode= httpConnection.getResponseCode() ;

if (resposnseCode == HttpURLConnection.HTTP_OK) {

InputStream in = httpConnection.getInputStream();

DocumentBuilderFactory dbf ;
dbf = DocumentBuilderFactory.newInstance();

DocumentBuilder db = dbf.newDocumentBuilder();


// Parse the Earthquakes entry 
Document dom = db.parse(in);
Element docEle = dom.getDocumentElement();

//ArrayList<Album> Albums = new ArrayList<Album>();


/* Returns a NodeList of all descendant Elements with a given tag name, in document order.*/

NodeList nl = docEle.getElementsByTagName("entry");

if (nl!=null && nl.getLength() > 0) {        
   for (int i = 0; i < nl.getLength(); i++) {

Element entry = (Element)nl.item(i);

/* Now on every property in Entry **/

Element title =(Element)entry.getElementsByTagName("title").item(0);          

*Here i Get an Error*
String album_Title = title.getTextContent();

Element id =(Element)entry.getElementsByTagName("id").item(0);         
String album_id = id.getTextContent(); // 

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

吹泡泡o 2024-10-17 06:13:16

API 7 (Android 2.1) 不支持 getTextContent()。它是在 API 8 (2.2) 中引入的。

假设服务器的结果是可预测的,您可以使用以下代码:

Node n = aNodeList.item(i);

String strValue = n.getFirstChild().getNodeValue();

// as opposed to the String strValue = n.getTextContent();

如果元素可能为空,那么您需要首先检查子计数。

getTextContent() is not supported in API 7 (Android 2.1). It was introduced in API 8 (2.2).

Assuming a predictable result from the server, you can use the following code:

Node n = aNodeList.item(i);

String strValue = n.getFirstChild().getNodeValue();

// as opposed to the String strValue = n.getTextContent();

If the element may be empty, then you'd want to check the child count first.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文