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?

0


a source to share


4 answers


To render the html entry from xml, its called XSLT , which is a style sheet language for XML, its way to convert the xml file to display as html, you can use things like Dreamweaver to help you edit and do the transformation.



How to contrast java; The DOM parser loads an XML file into memory and creates an object model. Below is a quick example on how you can do this.

+4


a source


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



+2


a source


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

      

+2


a source


This code snippet can clarify things for you using XSLT and Java (JSTL), just complementing the good references 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:set var="xslDoc">
    <?xml version="1.0"?>
    <xsl:stylesheet version="1.0"
      xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
        <xsl:template match="/">
          <html>
          <body>
            <h2>My CD Collection</h2>
            <table border="1">
              <tr bgcolor="#9acd32">
                <th>Title</th>
                <th>Artist</th>
              </tr>
              <xsl:for-each select="catalog/cd">
                <tr>
                  <td><xsl:value-of select="title"/></td>
                  <td><xsl:value-of select="artist"/></td>
                </tr>
              </xsl:for-each>
            </table>
          </body>
          </html>
        </xsl:template>
    </xsl:stylesheet>
</c:set>

<c:set var="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:transform xml="${xmlDoc}" xslt="${xslDoc}" />

      

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

+1


a source







All Articles