How to get the root node attribute on Java

I have an XML file, as shown below I want to get the latitude and longitude attributes of the pharmacy node I can get the child nodes attribute, but I can't get the root node attribute I am the new Java and XML I can't find a solution how to do it

<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>

This is my code part I get the XML file from a URL address

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"));

                    ....

Solution

The < pharmacy > element itself can be used

Element pharmacies = doc.getDocumentElement();

You can get attributes from it

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>