0

How can we insert a record into an XML file using Java?

How can we display one record from this XML file using HTML?

3
  • Why are y'all downvoting this? It's a programming question, right? Commented May 24, 2009 at 15:58
  • I don't know, because the question is not clear at all ? A minimum effort should be done on the question before asking it. Commented May 24, 2009 at 16:51
  • 1
    Maybe he just isn't english fluent. Commented May 24, 2009 at 18:44

4 Answers 4

4

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.

Sign up to request clarification or add additional context in comments.

Comments

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

Comments

2
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>");

Comments

1

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: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 for making this in a servlet or a business class, I like Apache Xalan

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.