1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43
| package fr.guigui.test;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
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.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;
public class Parser {
private File file = new File("question.xml");
public Parser() throws ParserConfigurationException, FileNotFoundException, SAXException, IOException {
// TODO Auto-generated constructor stub
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
Document dom = builder.parse(new FileInputStream(file));
Element root = dom.getDocumentElement();
NodeList items = root.getElementsByTagName("question");
for(int i = 0; i < items.getLength(); i++){
Node item = items.item(i);
String value = item.getAttributes().getNamedItem("value").getNodeValue();
System.out.println("affichage contenu récupérer du fichier xml : "+value);
}
}
} |
Partager