2011-01-19 13 views
11

私は以下のようなxmlファイルを持っています。私は薬局ノードの緯度と経度の属性を取得したいと思います。私はchilnodes属性を取得できますが、ルートノードの属性を取得できませんでした。私はjavaとxmlの新機能です。私はどのように解決策を見つけることができませんでした。Javaでルートノードの属性を取得する方法

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

        .... 

答えて

23

<pharmacies>要素自体は、あなたがそれから属性を取得することができます

Element pharmacies = doc.getDocumentElement(); 

を使用して取得することができます。

0

は、あなたは、このサイトで多くの例を見ることができ、ルートノードpharmacies.And利用getAttributesメソッドinstead.Youの属性を取得する必要がある場合は、代わりに薬局の薬局を使用する必要があります。 http://java.sun.com/developer/codesamples/xml.html#dom

5

doc.getDocumentElement()はルート要素を返し、他の要素と同じようにgetAttribute(attrName)を呼び出すことができます。

2

次のことを試してください。

DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); 
    DocumentBuilder db = dbf.newDocumentBuilder(); 
    Document doc = db.parse(new ByteArrayInputStream(xml.getBytes("UTF-8"))); 
    doc.getDocumentElement().normalize(); 
    System.out.println(doc.getChildNodes().getLength()); 
    Node item = doc.getChildNodes().item(0); 
    System.out.println(item.getNodeName()); 
    Node lat = item.getAttributes().getNamedItem("latitude"); 
    String s = lat.getNodeValue(); 
    System.out.println(s.equals("36.8673380")); // Value of /pharmacies[@latitude]/value() 
+0

はい、それはSkaffmanの答えと同じように機能しています。私を助けてくれてありがとう! – jharry

0

RESがあなたの最後の文字列で、私にとってその作業をしてみてください。

doc = b.parse(new ByteArrayInputStream(result.getBytes("UTF-8"))); 

    Node rootNode=doc.getDocumentElement(); 

    res = rootNode.getNodeName().toString(); 
+0

親愛なる@ user3153156、これはrootNodeの名前だけを提供します。ここでの要件は、 "latitude"& "longitude"という名前のrootNodeの属性を取得することです。 :) –

0

<pharmacies>は要素&が

Element pharmacies = doc.getDocumentElement();を使用して得ることができ、それ自体であります

今このpharmaciesの参照変数の要素は、<pharmacies>要素のすべての属性を保持します。

pharmacies.getAttribute("latitude"); // Will give 36.8673380 
pharmacies.getAttribute("longitude"); // Will give 30.6346640 
関連する問題