如何在java上获取根节点属性
发布时间:2020-12-14 05:08:26 所属栏目:Java 来源:网络整理
导读:我有一个xml文件,如下所示.我想要得到药店节点的纬度和经度属性.我可以获得chilnodes属性,但不能得到根节点属性.我是新的 java和xml.我找不到解决方案如何做. pharmacies Acc="4" latitude="36.8673380" longitude="30.6346640" address="Ayujkila" pharmacy
我有一个xml文件,如下所示.我想要得到药店节点的纬度和经度属性.我可以获得chilnodes属性,但不能得到根节点属性.我是新的
java和xml.我找不到解决方案如何做.
<pharmacies Acc="4" latitude="36.8673380" longitude="30.6346640" address="Ayujkila"> <pharmacy name="sadde" owner="" address="dedes" distance="327.000555668" phone="342343" lat="36.8644" long="30.6345" accuracy="8"/> <pharmacy name="Sun " owner="" address="degerse" distance="364.450016586" phone="45623" lat="36.8641" long="30.6353" accuracy="8"/> <pharmacy name="lara" owner="" address="freacde" distance="927.262190129" phone="564667" lat="36.8731" long="30.6422" accuracy="8" <end/> </pharmacies> 这是我的代码部分.我从一个url地址获取xml文件. DocumentBuilderFactory dbf =DocumentBuilderFactory.newInstance(); DocumentBuilder db = dbf.newDocumentBuilder(); Document doc = db.parse(new InputSource(url.openStream())); doc.getDocumentElement().normalize(); NodeList nodeList =doc.getElementsByTagName("pharmacy"); for (int i = 0; i < nodeList.getLength(); i++){ Node node =nodeList.item(i); Element fstElmnt = (Element) node; NodeList pharmacyList = fstElmnt.getElementsByTagName("pharmacy"); Element pharmacyElement = (Element) pharmacyList.item(0); Element pharmacyElement = (Element) pharmacyList.item(0); HashMap<String,String>map=new HashMap<String,String>(); map.put("name",pharmacyElement.getAttribute("name")); map.put("distance",pharmacyElement.getAttribute("phone")); list.add(map); latt.add(pharmacyElement.getAttribute("lat")); .... 解决方法
<药店>元素本身可以使用
Element pharmacies = doc.getDocumentElement(); 你可以从中获得属性. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
相关内容
- IntelliJ IDEA使用maven实现tomcat的热部署
- Java:正则表达式取代
- java利用Apache commons codec进行MD5加密,BASE64加密解密,
- 基于spring+quartz的分布式定时任务框架实现
- springboot+oauth2.0异常重写处理(针对token失效)
- java – 为什么这个Google AppEngine Search API查询没有找
- How do annotations work internally--转
- 如何在Java Applet中注册JavaScript回调?
- Java后台技术(TDDL)
- java中将科学计数法转换普通计数法的简单方法