2012-05-03 36 views
0

我的XML文件是這樣的:如何從xml文件中獲取屬性?

<?xml version="1.0" encoding="UTF-8"?> 
<!DOCTYPE pointList SYSTEM "point.dtd"> 
<pointList> 
<point unit="mm"> 
<x>2</x> 
<y>3</y> 
</point> 

<point unit="cm"> 
<x>9</x> 
<y>3</y> 
</point> 

<point unit="px"> 
<x>4</x> 
<y>7</y> 
</point> 

</pointList> 

當我嘗試獲取標記點的屬性:

import java.io.File; 
import java.io.IOException; 


import javax.xml.parsers.DocumentBuilder; 
import javax.xml.parsers.DocumentBuilderFactory; 
import javax.xml.parsers.ParserConfigurationException; 

import org.w3c.dom.Document; 
import org.w3c.dom.Element; 
import org.w3c.dom.NamedNodeMap; 
import org.w3c.dom.Node; 
import org.w3c.dom.NodeList; 
import org.xml.sax.SAXException; 


public class TryXml { 
    public static void main (String [] args) { 
     DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); 
     DocumentBuilder builder = null; 
     Document doc = null; 
     try { 
      builder = factory.newDocumentBuilder(); 
     } 
     catch (ParserConfigurationException e){ 
      System.out.println(e.getMessage()); 
     } 
     File f = new File("p1.xml"); 
     try { 
      doc=builder.parse(f); 
     } 
     catch (SAXException e){ 
      e.printStackTrace(); 
     } 
     catch (IOException e){ 
      e.printStackTrace(); 
     } 

     Element root = doc.getDocumentElement(); 
     System.out.println(root.getTagName()); 

     System.out.println("****************"); 


     NodeList nList = root.getChildNodes(); 
     for (int i=0;i<nList.getLength();i++) { 
     if(nList.item(i) instanceof Element) 

     System.out.println(nList.item(i).getAttributes()); 
    } 

    } 
    } 

我得到的是類似地址:

[email protected] 
[email protected] 
[email protected] 

能任何人都會給我一個關於如何獲得點和其他內部標籤的屬性的提示?

回答

1

你可以用下面的更換你的println:

System.out.println(((Element) nList.item(i)).getAttribute("unit")); 

這應該給你當前元素的「單位」屬性。

+0

但是,如果我使用「單位」我如何獲得內部標籤的值?我需要的結果是這樣的:'點23毫米,點93釐米,點47px' – Gipsy

+1

你現在有'元素',並可以從那裏工作你的方式通過孩子:'((Element)nList.item(i)) .getChildNodes()'會爲你提供另一個'NodeList',通過它可以完全按照你在代碼中通過'nList'進行迭代。 – Oliver

+0

@Gipsy在我的答案中看到編輯。 –

0

使用,

「元素根= doc.getElementByTagName(」 pointList 「);」

代替,

「元素根= doc.getDocumentElement();」

0

Element.getAttributes()將列出該Element中的所有屬性。如果您事先知道屬性的名稱並只想獲取其值,請改用Element.getAttribute(String)

如果您需要獲取子元素,請使用Element.getChildNodes()。要獲得Element或更具體地說Node中的文字,請使用getNodeValue()

例如:

 NodeList nList = root.getChildNodes(); 
     String out = ""; 
     for (int i=0;i<nList.getLength();i++) { 
      // Assuming all first-level tags are <point> 
      Element point = (Element) nList.item(i); 
      String unit = point.getAttribute("unit"); 
      out += "point "; 
      for (int y=0;y<point.getChildNodes().getLength();y++){ 
       Node child = point.getChildNodes().item(y); 
       // String nodeName = child.getNodeName(); 
       String nodeValue = child.getNodeValue(); 
       out += nodeValue; 
      } 
     out += unit; 
     out += ", "; 
     } 

將輸出point 23mm, point 93cm, point 47px,

+0

我在System.out.println()中放什麼?我應該把代碼放在哪裏?還有'Element point =(Element)nList.item(i); '說不能轉換成org.w3c.dom.Element – Gipsy