Lire un fichier XML en java, DOM

Akrouti Ezzeddin Messages postés 50 Date d'inscription   Statut Membre Dernière intervention   -  
Akrouti Ezzeddin Messages postés 50 Date d'inscription   Statut Membre Dernière intervention   -
Bonjour,
je veux lire un fichier XML suivante:
copie.xml
<?xml version="1.0">
<ClientInfos>
<RSSI>13</RSSI>
<BER>99</BER>
<CI>25</CI>
<LAC>270011</LAC>
<Date>2012-04-26 13:32:58</Date>
</ClientInfos>





voila le code:
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.DocumentBuilder;
import org.w3c.dom.Document;
import org.w3c.dom.NodeList;
import org.w3c.dom.Node;
import org.w3c.dom.Element;
import java.io.File;
 
public class ReadXMLFile {
 
	public static void main(String argv[]) {
 
	  try {
 
		File fXmlFile = new File("C:\\copie.xml");
		DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
		DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
		Document doc = dBuilder.parse(fXmlFile);
		doc.getDocumentElement().normalize();
 
		System.out.println("Root element :" + doc.getDocumentElement().getNodeName());
		NodeList nList = doc.getElementsByTagName("client");
		System.out.println("-----------------------");
 
		for (int temp = 0; temp < nList.getLength(); temp++) {
 
		   Node nNode = nList.item(temp);
		   if (nNode.getNodeType() == Node.ELEMENT_NODE) {
 
		      Element eElement = (Element) nNode;
 
		      System.out.println("RSSI: " + getTagValue("RSSI", eElement));
		      System.out.println("BER : " + getTagValue("BER", eElement));
	          System.out.println("CI : " + getTagValue("CI", eElement));
		      System.out.println("LAC : " + getTagValue("LAC", eElement));
		      System.out.println("Date : " + getTagValue("Date", eElement));
		   }
		}
	  } catch (Exception e) {
		e.printStackTrace();
	  }
  }
 
  private static String getTagValue(String sTag, Element eElement) {
	NodeList nlList = eElement.getElementsByTagName(sTag).item(0).getChildNodes();
 
        Node nValue = (Node) nlList.item(0);
 
	return nValue.getNodeValue();
  }
 
}


Merci pour vos idées
A voir également:

1 réponse

Akrouti Ezzeddin Messages postés 50 Date d'inscription   Statut Membre Dernière intervention  
 
c'est bon résolu!!!!
0