XML 设计 - 性能和效率

XML Design - Performance and Efficiency

我正在为类别制作一个 XML 地图文件。我想知道考虑我想到的两个选项时正确的方法是什么。

我的主要问题是 php 代码的 speed/efficiency 可能必须读取 XML 文件。 所以这是我的第一种方法

<?xml version="1.0" encoding="utf-8"?>
<category-map lang="en_US" reader="Candor Framework">

    <for-her url="/content/syl/tags/for-her">

        <women-tops url="/content/syl/category/15/women-tops">
            <women-longsleeve-tops url="/content/syl/tags/women-longsleeve-tops">Longsleeve Tops</women-longsleeve-tops>
            <women-shortsleeve-tops url="/content/syl/tags/women-shortsleeve-tops">Shortsleeve Tops</women-shortsleeve-tops>
        </women-tops>
        <women-gowns url="/content/syl/category/18/women-gowns/">
            <short-gowns url="/content/syl/tags/short-gowns">Short Gowns</short-gowns>
            <evening-gowns url="/content/syl/tags/evening-gowns">Evening Gowns</evening-gowns>
        </women-gowns>
        <women-shoes url="/content/syl/category/19/women-shoes/">
            <women-highheel-shoes url="/content/syl/tags/women-highheel-shoes">High Heel Shoes</women-highheel-shoes>
            <women-wedge-shoes url="/content/syl/tags/women-wedge-shoes">Wedge Shoes</women-wedge-shoes>
        </women-shoes>
    </for-her>

    <for-him url="/content/syl/tags/for-him">
        <men-shirt url="/content/syl/category/20/men-shirt">
          <men-lonsleeve-shirt url="/content/syl/tags/men-longsleeve-shirt">Longsleeve Shirt</women-longsleeve-tops>
        </men-shirt>
    </for-him>

</category-map>

使用这种方法,所有 XML 标签都有唯一的名称。现在与第二种方法进行比较。

 <?xml version="1.0" encoding="utf-8"?>
<category-map lang="en_US" reader="Candor Framework">

    <target name="for-her" url="/content/syl/tags/for-her">

        <category name="women-tops" url="/content/syl/category/15/women-tops">
            <tag name="women-longsleeve-tops" url="/content/syl/tags/women-longsleeve-tops">Longsleeve Tops</tag>
            <tag name="women-shortsleeve-tops" url="/content/syl/tags/women-shortsleeve-tops">Shortsleeve Tops</tag>
        </category>
        <category name="women-gowns" url="/content/syl/category/18/women-gowns/">
            <tag name="short-gowns" url="/content/syl/tags/short-gowns">Short Gowns</tag>
            <tag name="evening-gowns" url="/content/syl/tags/evening-gowns">Evening Gowns</tag>
        </category>
        <category name="women-shoes" url="/content/syl/category/19/women-shoes/">
            <tag name="women-highheel-shoes" url="/content/syl/tags/women-highheel-shoes">High Heel Shoes</tag>
            <tag name="women-wedge-shoes" url="/content/syl/tags/women-wedge-shoes">Wedge Shoes</tag>
        </category>
    </target>

    <target name="for-him" url="/content/syl/tags/for-him">
        <category name="men-shirt" url="/content/syl/category/20/men-shirt">
          <tag name="men-lonsleeve-shirt" url="/content/syl/tags/men-longsleeve-shirt">Longsleeve Shirt</tag>
        </category>
    </target>

</category-map>

这是 XML 文件的简短版本。整个内容还是蛮多的。这些设计方法中哪种更快?哪种需要更少的循环和条件?建议遵循哪一个?为什么?

非常感谢!

在我看来,我会说第二个最好,纯粹是因为它更通用,因此无需更新即可扩展。

我将 XML 视为一种代码与其他代码块进行通信的机制,因此该结构应该反映最适合代码的结构,而不一定要有任何使人更容易理解的结构读。对我来说,第一个 XML 看起来像是为人而不是计算机编写的,而第二个更符合数据库存储信息的方式:即 select 行,其中目标 == for-him and catagory == men-shirt.

就性能而言,我想它们是等价的,因为您可能会发现它们的代码相同,因为第一个 XML 实际上是使用元素名称而不是名称属性。如果您的代码是可扩展的,可以在第一种情况下添加新元素,则情况尤其如此。

如果您有大量数据,请考虑将其存储在数据库中,而不是像 sqlite 那样使用反映您的第二个 XML 的架构。然后,您可以为任何客户将查询结果导出到 XML 或 JSON。