i have an xml file and i use dom parseur to get elements . when i try this code with system.out.println ( in : System.out.println("Marks : " + eElement .getElementsByTagName("rating") .item(0) .getTextContent());) i receive all elements in the xml file , but my problem is when i want to put this elements in a text area jTextArea1.setText( eElement .getElementsByTagName("name") .item(0) .getTextContent());
, i only obtain the last element in xml file , not all elements like system.out.println , please help me i want to get all elements from xml file and put them in a text area
this is my code :
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
String h; try { File inputFile = new File("src/josephXml.xml"); DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance(); DocumentBuilder dBuilder = dbFactory.newDocumentBuilder(); Document doc = dBuilder.parse(inputFile); doc.getDocumentElement().normalize(); NodeList nList = doc.getElementsByTagName("Restaurant"); for (int temp = 0; temp < nList.getLength(); temp++) { Node nNode = nList.item(temp); if (nNode.getNodeType() == Node.ELEMENT_NODE) { Element eElement = (Element) nNode; jTextArea1.setText( "Name : "+ eElement .getElementsByTagName("name") .item(0) .getTextContent()+"\n " +"Location : "+ eElement .getElementsByTagName("location") .item(0) .getTextContent()+"\n" +eElement .getElementsByTagName("phone") .item(0) .getTextContent()+"\n" + eElement .getElementsByTagName("cuisine") .item(0) .getTextContent()+"\n" + eElement .getElementsByTagName("rating") .item(0) .getTextContent()); } } } catch (Exception e) { e.printStackTrace(); }
and this a part from my xml file
<Restaurants> <Restaurant> <name >Casablanca</name> <location>Ain el Mreysseh</location> <phone>+9611369334</phone> <cuisine>international cuisine</cuisine> <rating>4.8/5</rating> </Restaurant> <Restaurant> <name>Sydney's Club</name> <location>Minet el Hosn</location> <phone>+9611369280</phone> <cuisine>international cuisine</cuisine> <rating>4.7/5</rating> </Restaurant> <Restaurant> <name>Burgundy</name> <location>Saifi Village</location> <phone>+9611099982</phone> <cuisine>international,french cuisine</cuisine> <rating>4.7/5</rating> </Restaurant>
No comments:
Post a Comment