久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

如何獲取具有特定屬性值的特定 XML 元素?

How to get specific XML elements with specific attribute value?(如何獲取具有特定屬性值的特定 XML 元素?)
本文介紹了如何獲取具有特定屬性值的特定 XML 元素?的處理方法,對(duì)大家解決問(wèn)題具有一定的參考價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)吧!

問(wèn)題描述

我正在嘗試通過(guò)獲取參數(shù) type_id="4218"?? 的所有<Type>"元素從 URL 解析 XML 文件?

XML 文檔:

<BSQCUBS Version="0.04" Date="Fri Dec 9 11:43:29 GMT 2011" MachineDate="Fri, 09 Dec 2011 11:43:29 +0000"><類class_id="385"><標(biāo)題>足球比賽</標(biāo)題><類型 type_id="4264" type_minbet="0.1" type_maxbet="2000.0">...</類型><類型 type_id="5873" type_minbet="0" type_maxbet="0">...</類型><類型 type_id="4725" type_minbet="0.1" type_maxbet="2000.0">...</類型><類型 type_id="4218" type_minbet="0.1" type_maxbet="2000.0">...</類型><類型 type_id="4221" type_minbet="0.1" type_maxbet="2000.0">...</類型><類型 type_id="4218" type_minbet="0.1" type_maxbet="2000.0">...</類型><類型 type_id="4299" type_minbet="0.1" type_maxbet="2000.0">...</類型></類></BSQCUBS>

這是我的 Java 代碼:

 DocumentBuilder db = dbf.newDocumentBuilder();文檔 doc = db.parse(new URL("http://cubs.bluesq.com/cubs/cubs.php?action=getpage&thepage=385.xml").openStream());doc.getDocumentElement().normalize();NodeList nodeList = doc.getElementsByTagName("Type");System.out.println("ukupno:"+nodeList.getLength());if (nodeList != null && nodeList.getLength() > 0) {for (int j = 0; j < nodeList.getLength(); j++) {元素 el = (org.w3c.dom.Element) nodeList.item(j);type_id = Integer.parseInt(el.getAttribute("type_id"));System.out.println("類型id:"+type_id);}}

這段代碼給了我所有元素,我不想要那個(gè),我想要屬性 type_id = "4218" 的所有元素!

解決方案

XPath 是你的正確選擇:

DocumentBuilderFactory 工廠 = DocumentBuilderFactory.newInstance();DocumentBuilder builder = factory.newDocumentBuilder();Document doc = builder.parse("<你的 xml doc uri>");XPathFactory xPathfactory = XPathFactory.newInstance();XPath xpath = xPathfactory.newXPath();XPathExpression expr = xpath.compile("http://Type[@type_id="4218"]");NodeList nl = (NodeList) expr.evaluate(doc, XPathConstants.NODESET);

并遍歷nl

I am trying to parse an XML file from a URL by taking all "<Type>" elements where is parameter type_id="4218"??

XML document:

<BSQCUBS Version="0.04" Date="Fri Dec 9 11:43:29 GMT 2011" MachineDate="Fri, 09 Dec 2011 11:43:29 +0000">
  <Class class_id="385">
    <Title>Football Matches</Title>
    <Type type_id="4264" type_minbet="0.1" type_maxbet="2000.0">
      ...
    </Type>
    <Type type_id="5873" type_minbet="0" type_maxbet="0">
      ...
    </Type>
    <Type type_id="4725" type_minbet="0.1" type_maxbet="2000.0">
      ...
    </Type>
    <Type type_id="4218" type_minbet="0.1" type_maxbet="2000.0">
      ...
    </Type>
    <Type type_id="4221" type_minbet="0.1" type_maxbet="2000.0">
      ...
    </Type>
    <Type type_id="4218" type_minbet="0.1" type_maxbet="2000.0">
      ...
    </Type>
    <Type type_id="4299" type_minbet="0.1" type_maxbet="2000.0">
      ...
    </Type>
  </Class>
</BSQCUBS>

Here is my Java code:

 DocumentBuilder db = dbf.newDocumentBuilder();
 Document doc = db.parse(new URL("http://cubs.bluesq.com/cubs/cubs.php?action=getpage&thepage=385.xml").openStream());

 doc.getDocumentElement().normalize();

 NodeList nodeList = doc.getElementsByTagName("Type");
 System.out.println("ukupno:"+nodeList.getLength());
 if (nodeList != null && nodeList.getLength() > 0) {
   for (int j = 0; j < nodeList.getLength(); j++) {
     Element el = (org.w3c.dom.Element) nodeList.item(j);
     type_id = Integer.parseInt(el.getAttribute("type_id"));
     System.out.println("type id:"+type_id);
   }
 }

This code gives me all elements, I don't want that, I want all elements where the attribute type_id = "4218"!

解決方案

XPath is right choice for you:

DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
Document doc = builder.parse("<Your xml doc uri>");
XPathFactory xPathfactory = XPathFactory.newInstance();
XPath xpath = xPathfactory.newXPath();
XPathExpression expr = xpath.compile("http://Type[@type_id="4218"]");
NodeList nl = (NodeList) expr.evaluate(doc, XPathConstants.NODESET);

And iterate through nl

這篇關(guān)于如何獲取具有特定屬性值的特定 XML 元素?的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!

【網(wǎng)站聲明】本站部分內(nèi)容來(lái)源于互聯(lián)網(wǎng),旨在幫助大家更快的解決問(wèn)題,如果有圖片或者內(nèi)容侵犯了您的權(quán)益,請(qǐng)聯(lián)系我們刪除處理,感謝您的支持!

相關(guān)文檔推薦

Upload progress listener not fired (Google drive API)(上傳進(jìn)度偵聽(tīng)器未觸發(fā)(Google 驅(qū)動(dòng)器 API))
Save file in specific folder with Google Drive SDK(使用 Google Drive SDK 將文件保存在特定文件夾中)
Google Drive Android API - Invalid DriveId and Null ResourceId(Google Drive Android API - 無(wú)效的 DriveId 和 Null ResourceId)
Google drive api services account view uploaded files to google drive using java(谷歌驅(qū)動(dòng)api服務(wù)賬戶查看上傳文件到谷歌驅(qū)動(dòng)使用java)
Google Drive service account returns 403 usageLimits(Google Drive 服務(wù)帳號(hào)返回 403 usageLimits)
com.google.api.client.json.jackson.JacksonFactory; missing in Google Drive example(com.google.api.client.json.jackson.JacksonFactory;Google Drive 示例中缺少)
主站蜘蛛池模板: jizjizjiz中国护士18 | 日韩精品在线视频 | 成人超碰| 在线中文视频 | 波多野结衣在线观看一区二区三区 | 日韩视频在线播放 | 北条麻妃一区二区三区在线视频 | 奇米影视首页 | 免费看淫片 | 天天拍夜夜爽 | 欧美a区| 黄色国产视频 | 精品国产青草久久久久96 | 色在线免费 | 日本成人中文字幕 | 成人网在线 | 伊人激情综合网 | 欧美一级免费看 | www.日韩在线 | 中文字幕亚洲视频 | 亚洲一二三区精品 | 一级国产精品一级国产精品片 | 欧美一区二区三区在线看 | 亚洲精品1区 | 国产精品夜色一区二区三区 | 青青艹在线视频 | 日韩av一区在线观看 | 亚洲欧美综合精品另类天天更新 | 久久精品无码一区二区三区 | 国产毛片视频 | 亚洲一区二区三区免费在线观看 | 精品99在线 | 91看片免费版 | 国产又爽又黄的视频 | 97人人超碰 | 久久香蕉精品视频 | 国产一区二区三区视频 | 成人国产综合 | 欧美一区二区三区大片 | 日本二区在线观看 | 欧美精品一区二区三区在线 |