在线xml到html解析器

时间:2013-10-23 05:46:23

标签: html xml parsing

我正在寻找一个解析任何xml到html的在线程序。例如,如果我有以下xml布局

<users>
    <user>
         </name>john</name>
         <pictures>
              <pic1>URL of picture1.png</pic1>
              <pic2>URL of picture2.png</pic2>
         </pictures>
    </user>
    <user>
         </name>mary</name>
         <pictures>
              <pic1>URL of picture1.png</pic1>
              <pic2>URL of picture2.png</pic2>
         </pictures>
    </user>
</users>

它可以创建一个显示节点内容的html页面,并提供一些最小的格式以便于阅读。任何这样的工具?

编辑:我刚刚给出了一个例子,我想要一个可以解析任何xml但不事先知道其结构的通用工具。

2 个答案:

答案 0 :(得分:0)

解决这个问题的一种方法是应用XSLT转换。

您需要创建一个XSL样式表,例如......

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<html>
<head>
    <title>Contacts</title>
</head>
<body>
    <xsl:for-each select="//user">
        <h2><xsl:value-of select="name" /></h2>
        <p><xsl:value-of select="pictures/pic1" /></p>
        <p><xsl:value-of select="pictures/pic2" /></p>
    </xsl:for-each>
</body>
</html>
</xsl:template>
</xsl:stylesheet> 

然后在xml文件的顶部链接到它。在下面的示例中,第二行链接到名为“myStyleSheet.xsl”的样式表。

<?xml version="1.0" encoding="utf-8"?>
<?xml-stylesheet type="text/xsl" href="myStyleSheet.xsl"?>
<users>
    <user>
         <name>john</name>
         <pictures>
              <pic1>URL of picture1.png</pic1>
              <pic2>URL of picture2.png</pic2>
         </pictures>
    </user>
    <user>
         <name>mary</name>
         <pictures>
              <pic1>URL of picture1.png</pic1>
              <pic2>URL of picture2.png</pic2>
         </pictures>
    </user>
</users>

答案 1 :(得分:-1)

你可以试试这个。

http://www.w3schools.com/xsl/tryxslt.asp?xmlfile=cdcatalog&xsltfile=cdcatalog_choose

<?xml version="1.0" encoding="ISO-8859-1"?>
<!-- Edited by XMLSpy® -->
<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>
  <xsl:choose>
  <xsl:when test="price > 10">
     <td bgcolor="#ff00ff">
     <xsl:value-of select="artist"/>
     </td>
  </xsl:when>
  <xsl:otherwise>
     <td><xsl:value-of select="artist"/></td>
  </xsl:otherwise>
  </xsl:choose>
  </tr>
</xsl:for-each>
 </table>
</body>
</html>
</xsl:template>
</xsl:stylesheet>