|
|
|||||||||
|
|||||||||
| |||||||||
|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
#1
|
|||
|
|||
|
Loop through XML elements using HTML ONLY
Hello,
I am having difficulty finding help on this topic, as most people use XSL to display the tags (which is understandable). However, I am trying to minimize my work here and just display the XML elements in HTML. At this point, I am only able to display the first set of tags, so I need a means of looping through the rest of the xml file. Any ideas, anyone?? XML: <?xml version= "1.0" ?> <Research> <coral> <Family>Xeniidae</Family> <CommonName>Pumping Xenia</CommonName> <Light>Medium</Light> <Flow>Medium</Flow> <Care>Moderate</Care> </coral> <coral> <Family>Zoanthidae</Family> <CommonName>Colony Polyp</CommonName> <Light>Medium</Light> <Flow>Medium</Flow> <Care>Easy</Care> </coral> </Research HTML: <html> <head> <title>Fast, Accurate Research on Corals</title> </head> <body> <xml src="ResearchCorals.xml" id="refTable"></xml> <table border="1" style="font: 10pt Verdana, sans-serif; color:navy; background-color:rgb(255,255,200)" cellpadding="5" > <thead> <td>Family</td> <td>Light</td> <td>Water Flow</td> <td>Care Level</td> </thead> <td> <span datasrc="#refTable" datafld="Family"></span> </td> <td> <span datasrc="#refTable" datafld="Light"></span> </td> <td> <span datasrc="#refTable" datafld="Flow"></span> </td> <td> <span datasrc="#refTable" datafld="Care"></span> </td> </tr> </table> </body> </html> THANKS FOR THE HELP!! |
|
#2
|
||||
|
||||
|
Hmm... I would think XSL would be the easiest method.
Code:
<xsl:for-each select="Research/coral"> <xsl:value-of select="whatever you want here" /> </xsl:for-each>
__________________
Hello, old friend... |
|
#3
|
|||
|
|||
|
Thanks khwang, but I seemed to have figured it out without using xsl.
Here are the changes I made in the html file: <table border="1" style="font: 10pt Verdana, sans-serif; color:navy; background-color:rgb(102, 102, 255)" cellpadding="5" datasrc="#refTable" align="center"> <td> <span datafld="Family" ></span> </td> Thanks again! I appreciate the effort!! |
|
#4
|
|||
|
|||
|
hi man you can use this cod to loop in xml file
import java.io.File; import org.w3c.dom.Document; import org.w3c.dom.*; import javax.xml.parsers.DocumentBuilderFactory; import javax.xml.parsers.DocumentBuilder; import org.xml.sax.SAXException; import org.xml.sax.SAXParseException; public class ReadAndPrintXMLFiles{ public static void main (String argv []){ try { DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance(); DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder(); Document doc = docBuilder.parse (new File("book.xml")); // normalize text representation doc.getDocumentElement ().normalize (); System.out.println ("Root element of the doc is " + doc.getDocumentElement().getNodeName()); NodeList listOfPersons = doc.getElementsByTagName("person"); int totalPersons = listOfPersons.getLength(); System.out.println("Total no of people : " + totalPersons); for(int s=0; s<listOfPersons.getLength() ; s++){ Node firstPersonNode = listOfPersons.item(s); if(firstPersonNode.getNodeType() == Node.ELEMENT_NODE){ Element firstPersonElement = (Element)firstPersonNode; //------- NodeList firstNameList = firstPersonElement.getElementsByTagName("first"); Element firstNameElement = (Element)firstNameList.item(0); NodeList textFNList = firstNameElement.getChildNodes(); System.out.println("First Name : " + ((Node)textFNList.item(0)).getNodeValue().trim()); //------- NodeList lastNameList = firstPersonElement.getElementsByTagName("last"); Element lastNameElement = (Element)lastNameList.item(0); NodeList textLNList = lastNameElement.getChildNodes(); System.out.println("Last Name : " + ((Node)textLNList.item(0)).getNodeValue().trim()); //---- NodeList ageList = firstPersonElement.getElementsByTagName("age"); Element ageElement = (Element)ageList.item(0); NodeList textAgeList = ageElement.getChildNodes(); System.out.println("Age : " + ((Node)textAgeList.item(0)).getNodeValue().trim()); //------ }//end of if clause }//end of for loop with s var }catch (SAXParseException err) { System.out.println ("** Parsing error" + ", line " + err.getLineNumber () + ", uri " + err.getSystemId ()); System.out.println(" " + err.getMessage ()); }catch (SAXException e) { Exception x = e.getException (); ((x == null) ? e : x).printStackTrace (); }catch (Throwable t) { t.printStackTrace (); } //System.exit (0); }//end of main } see the xml file it work with java cod <?xml version="1.0"?> <book> <person> <first>Otman</first> <last>Elafi</last> <age>32</age> </person> <person> <first>Amal</first> <last>Elfituri</last> <age>23</age> </person> <person> <first>Tasneem</first> <last>Elafi</last> <age>2</age> </person> <person> <first>Kiran</first> <last>Pai</last> <age>22</age> </person> <person> <first>Bill</first> <last>Gates</last> <age>46</age> </person> <person> <first>Steve</first> <last>Jobs</last> <age>40</age> </person> </book> |
|
#5
|
|||
|
|||
|
Quote:
Ah...........I agree with khwang. You really want to invent another one? |
![]() |
| Viewing: Dev Shed Forums > Programming Languages - More > XML Programming > Loop through XML elements using HTML ONLY |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|