gpt4 book ai didi

java - 如何在java上获取根节点属性

转载 作者:搜寻专家 更新时间:2023-10-30 19:49:51 25 4
gpt4 key购买 nike

我有一个如下所示的 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"));

....

最佳答案

<pharmacies>元素本身可以使用

Element pharmacies = doc.getDocumentElement();

您可以从中获取属性。

关于java - 如何在java上获取根节点属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4734586/

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