Skip to content Skip to sidebar Skip to footer

How Can I Write To Xml Via Java And Display It Via Html?

How can we insert a record into an XML file using Java? How can we display one record from this XML file using HTML?

Solution 1:

To display a record of html from xml, its called XSLT, which is a stylesheet language for XML,its a way to transform an xml file to display as html, you can use things like Dreamweaver to help you edit and make the transformation.

As oppose to in java; DOM parser loads the XML file into the memory and makes an object model of it. Here is a quick Example on how you can do that.

Solution 2:

XML to HTML : use XSLT http://www.rgagnon.com/javadetails/java-0407.html inserting another Node in a XML tree: * use the DOM API and node.appendChild(newnode) : http://www.javazoom.net/services/newsletter/xmlgeneration.html * if your tree is too large, use the SAX API

Solution 3:

String xml = <learn to read file andget it asString>
xml = xml.trim().replaceAll("<","&lt;").replaceAll(">","&gt;");
os.println("<pre id=\"content\">" + xml + "</pre>");

Solution 4:

This code snippet may clarify things for you using XSLT and Java (JSTL), just complementing the good links Pierre and TStamper provided you

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/xml" prefix="x" %>

<c:setvar="xslDoc"><?xml version="1.0"?><xsl:stylesheetversion="1.0"xmlns:xsl="http://www.w3.org/1999/XSL/Transform"><xsl:templatematch="/"><html><body><h2>My CD Collection</h2><tableborder="1"><trbgcolor="#9acd32"><th>Title</th><th>Artist</th></tr><xsl:for-eachselect="catalog/cd"><tr><td><xsl:value-ofselect="title"/></td><td><xsl:value-ofselect="artist"/></td></tr></xsl:for-each></table></body></html></xsl:template></xsl:stylesheet></c:set><c:setvar="xmlDoc"><?xml version="1.0"?><catalog><cd><title>Stop</title><artist>Sam Brown</artist><country>UK</country><company>A and M</company><price>8.90</price><year>1988</year></cd><cd><title>Red</title><artist>The Communards</artist><country>UK</country><company>London</company><price>7.80</price><year>1987</year></cd></catalog></c:set><x:transformxml="${xmlDoc}"xslt="${xslDoc}" />

Also, there are many technologies for making this in a servlet or a business class, I like Apache Xalan

Post a Comment for "How Can I Write To Xml Via Java And Display It Via Html?"