Html 如何将xml数据解析成html?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10088637/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to parse the xml data into html?
提问by Sandy
Here is my xml:
这是我的 xml:
<Catalog>
<catalogDetail catalogId="DemoCatalog">
<catalogName>Demo Catalog</catalogName>
</catalogDetail>
<catalogDetail catalogId="GoogleCatalog">
<catalogName>Google Catalog</catalogName>
</catalogDetail>
</Catalog>
I want it to be read in HTML file how can I do this???
我想在 HTML 文件中读取它,我该怎么做???
回答by Luc125
To do this your HTML file should contain some JavaScript code, so you will want to learn how to parse XML in Javascript.
为此,您的 HTML 文件应包含一些 JavaScript 代码,因此您需要学习如何在 Javascript 中解析 XML。
Here is a good StackOverflow question on this topic: XML parsing in JavaScript
这是关于这个主题的一个很好的 StackOverflow 问题:XML parsing in JavaScript
回答by James Johnson
NOTE :If you can elaborate on what technology you're using, I'll try to provide a more complete example.
注意:如果您可以详细说明您使用的技术,我将尝试提供一个更完整的示例。
I would suggest using XSLT for this. With XSLT, you can pass in the XML fragment, parse it, and return formatted HTML.
我建议为此使用 XSLT。使用 XSLT,您可以传入 XML 片段、解析它并返回格式化的 HTML。
Here's an example XSLT document that converts XML to HTML:
下面是一个将 XML 转换为 HTML 的示例 XSLT 文档:
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:msxsl="urn:schemas-microsoft-com:xslt" exclude-result-prefixes="msxsl">
<xsl:output method="html" indent="yes"/>
<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>
回答by johndavedecano
You can do by using PHP's XML Library called simplexml for more information check this link http://www.w3schools.com/php/php_xml_simplexml.asp
您可以使用名为 simplexml 的 PHP 的 XML 库来获取更多信息,请查看此链接 http://www.w3schools.com/php/php_xml_simplexml.asp