Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

I want to link my XML file to my HTML page

Tags:

html

xml

I have created a XML file I would like for it to be displayed in my HTML page that I also created. Can someone tell me how to do this.

<?xml version="1.0"?>
<Family>
<Mom>Alison</Mom>
<age>44</age>
<son>Ian</son>
<age>8</age>
<son>Seth</son>
</Family>

I would like to read that from my html page

like image 924
user770022 Avatar asked Jan 13 '11 17:01

user770022


People also ask

Can you embed XML in HTML?

The unofficial <xml> tag is used to embed XML data within HTML. Note that the <xml> tag is an HTML element, not an XML element. Data Islands can be bound to HTML elements (like HTML tables). In the example below, an XML Data Island with an ID "cdcat" is loaded from an external XML file.

How do I add an XML file to my website?

Create a folder in your website called "XMLFiles". Create a file in that folder called "Videos. xml" and place your XML in the file. Save this answer.

How do XML and HTML work together?

HTML is used to display the data. XML is used to store data. HTML does not carry data it just display it. XML carries the data to and from database.


2 Answers

You can use XSLT - language for transforming XML documents. Maybe this would fit your needs.

I have modified your provided XML a little bit, because I think it is not structured well. So if we have such document:

<?xml version="1.0"?>
<?xml-stylesheet href="bla.xsl" type="text/xsl" ?>
<family>
   <person>
      <role>Mom</role>
      <name>Alison</name>
      <age>44</age>
   </person>
   <person>
      <role>Father</role>
      <name>Ben</name>
      <age>45</age>
   </person>
   <person>
      <role>Son</role>
      <name>Ian</name>
      <age>8</age>
   </person>
</family>

The XSLT file would look something like this:

<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<xsl:template match="/">
  <html>
  <body>
  <h2>Family</h2>
    <table border="1">
      <tr bgcolor="#9acd32">
    <th>Role</th>
        <th>Name</th>
        <th>Age</th>
      </tr>
      <xsl:for-each select="family/person">
          <tr>
        <td><xsl:value-of select="role"/></td>
        <td><xsl:value-of select="name"/></td>
        <td><xsl:value-of select="age"/></td>
          </tr>
      </xsl:for-each>
    </table>
  </body>
  </html>
</xsl:template>
</xsl:stylesheet>
like image 182
Robertas Avatar answered Oct 08 '22 18:10

Robertas


a) Simply linking your Xml file

You can link to your Xml file from a Html page by using Server Side Includes.

If your Webserver is configured to allow this feature (this is usually disabled for security reasons) all you need to do is to rename your Html page to .shtml and add the server side include command.

foo.shtml

<html>
    <head/>
    <body>
    <!--#include file="bar.xml" -->
    </body>
</html>

bar.xml

<?xml version="1.0"?>
<Family>
    <Mom>Alison</Mom>
    <age>44</age>
    <son>Ian</son>
    <age>8</age>
    <son>Seth</son>
</Family>

This will show the text Alison 44 Ian 8 Seth in your browser.


b) Rendering your Xml file as Html

If you want to render your complete Xml file as a Html page wenuxas has the correct answer for you.


c) Embedding your Xml file into your Html page

If your Xml document represents only a fragment of your final page Ajax may be what you are looking for.

like image 27
Filburt Avatar answered Oct 08 '22 18:10

Filburt