questo è quello che ho messo su:
il file xml è questo:codice:public static void read() throws ParserConfigurationException, SAXException, IOException { File fXmlFile = new File(System.getProperty("user.home") + "/todo.xml"); DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance(); DocumentBuilder dBuilder = dbFactory.newDocumentBuilder(); Document doc = dBuilder.parse(fXmlFile); doc.getDocumentElement().normalize(); NodeList nList = doc.getElementsByTagName("todos"); 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("Todo : " + getTagValue("todo", eElement)); } } } 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(); } public static void main(String[] args) throws ParserConfigurationException, SAXException, IOException { read(); }
il problema è che il metodo mi restituisce solo il mi primo todo.codice:<?xml version="1.0" encoding="UTF-8" standalone="no"?> <todos> <todo>yong</todo> <todo>yvbx</todo> <todo>yobxc</todo> <todo>yosdfgsdf</todo> </todos>

Rispondi quotando

