从 XML 生成 HTML 文件
我有一个带有矩形元素的 XML 文件,其中包含子元素 - RGBcolor、ID、高度和宽度。 现在我需要将矩形绘制到 HTML 网页中,并将提到的尺寸、颜色和名称绘制到几个 DIV 中。我该怎么做?我是这个行业的新手,所以请尽量详细说明。 这是我的 xml:
<?xml version="1.0"?>
<ArrayOfRectangle xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<Rectangle>
<Id>First one</Id>
<TopLeft>
<X>0.06</X>
<Y>0.4</Y>
</TopLeft>
<BottomRight>
<X>0.12</X>
<Y>0.13</Y>
</BottomRight>
<Color>
<Red>205</Red>
<Green>60</Green>
<Blue>5</Blue>
</Color>
</Rectangle>
<Rectangle>
<Id>Second One</Id>
XML 很好,我什至将其反序列化回来,但我不知道如何解析为 HTML。 谢谢大家
I have a XML file with Rectangle elements that contains sub-elements - RGBcolor,ID, height and width.
Now i need to draw the rectangles into a HTML web page with the mentioned sizes, color and name into several DIVs. how do i do it? I'm new in the business so please try to detail as much as you can.
here's my xml:
<?xml version="1.0"?>
<ArrayOfRectangle xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<Rectangle>
<Id>First one</Id>
<TopLeft>
<X>0.06</X>
<Y>0.4</Y>
</TopLeft>
<BottomRight>
<X>0.12</X>
<Y>0.13</Y>
</BottomRight>
<Color>
<Red>205</Red>
<Green>60</Green>
<Blue>5</Blue>
</Color>
</Rectangle>
<Rectangle>
<Id>Second One</Id>
The XML is fine and i even deserialized it back, but i dont know how to do the parsing into HTML..
Thank you all
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
简短的回答是你不能。 HTML 是一种文本标记语言,而不是图形语言。
您可以将所有规则转换为 CSS 并将其应用到div(通用块元素),尽管这可能是一种黑客行为。
SVG 是一种图形语言,它比您编写的语言得到更好的支持,但是 仍然有局限性。如果您将规则转换为 JavaScript,那么 Raphaël 可以根据不同浏览器的要求生成 SVG 或 VML。这将提供更广泛的支持,但代价是依赖 JS。
为了获得最广泛的支持,您可以在服务器上将 XML 文档转换为 PNG 图像。
如何从您的格式转换为另一种格式的具体细节有多种选择。您将需要一个 XML 解析器。然后你需要阅读文档并输出你喜欢的任何格式。为此,您可以使用 DOM,XSLT 是一个流行的选项(特别是对于 XML 到其他 XML 的转换)。
The short answer is that you can't. HTML is a text markup language, not a graphics language.
You could convert all your rules to CSS and apply them to divs (generic block elements), although that would be a hack.
SVG is a graphics language which is going to be better supported than your made up one, but still has limitations. If you convert your rules into JavaScript then Raphaël can generate SVG or VML as required by different browsers. This would give wider support at a cost of depending on JS.
For widest support, you could convert the XML document into a PNG image on the server.
The specifics of how you convert from your format to another are open to several options. You'll need an XML parser. Then you need to read the document and output whatever format you like. You could use DOM for this, and XSLT is a popular option (especially for XML to other XML translations).
我不清楚你的问题是设计你想要生成的 HTML(和/或 SVG),还是决定使用什么技术进行转换。对于简单的矩形,当然可以使用具有适当 CSS 属性的 HTML DIV 元素,但 SVG 为您提供了更大的灵活性。无论哪种方式,我都会使用 XSLT 进行转换;但请花一些时间阅读该语言并熟悉它,因为它可能与您以前使用过的任何语言都不同。
It's not clear to me whether your problem is designing the HTML (and/or SVG) that you want to generate, or in deciding what technology to use for the transformation. For simple rectangles, it's certainly possible to us HTML DIV elements with appropriate CSS properties, but SVG gives you more flexibility. Either way, I would use XSLT for the transformation; but allow some time to read up about the language and become familiar with it, because it's probably unlike anything you have used before.