从变量产品列表中获取非变量数据

时间:2020-02-11 22:25:12

标签: xml-parsing simplexml

我想在下面的一节中使用不带任何变体的xml数据。

<Root>
<Products>
    <Product>
        <Code>1472</Code>
        <Title>Same title...</Title>
        <Desc>Same title...</Desc>
                        <Pictures><Picture>https://a5bd.jpg</Picture></Pictures>
        <Stocks>
            <Stock>
                <Quantity>5</Quantity>
                <StockCode>1152White</StockCode>
                <Attribute name="COLOR">WHITE</Attribute>
                <Attribute name="SIZE">S</Attribute>
            </Stock>
            <Stock>
                <Quantity>0</Quantity>
                <StockCode>1152White</StockCode>
                <Attribute name="COLOR">WHITE</Attribute>
                <Attribute name="SIZE">M</Attribute>
            </Stock>
        </Stocks>
    </Product>
</Products>

我要实现的模式如下:

<Root>
    <Products>
        <Product>
            <ProductCode>1152White-S</ProductCode>
            <Title>Same title...</Title>
            <Desc>Same title...</Desc>
                        <Picture>https://a5bd.jpg</Picture>
            <Quantity>5</Quantity>
        </Product>
        <Product>
            <ProductCode>1152White-M</ProductCode>
            <Title>Same title...</Title>
                        <Picture>https://a5bd.jpg</Picture>
            <Desc>Same title...</Desc>
            <Quantity>0</Quantity>
        </Product>
    </Products>
</Root>

如果您分享您的想法,我会很高兴。

$url = 'https://xxmladdress.org';
$xml = simplexml_load_file($url);

$echo = "<products>";

$list = $xml->Products->Product;

for ($i = 0; $i < count($list); $i++) {
$echo .= "<ProductCode>". $list[$i]->Stocks->Stock->StockCode ."-". $list[$i]->Stocks->Stock->Attribute->SIZE ."</ProductCode>";
$echo .= "<Title>". $list[$i]->Title ."</Title>";
$echo .= "<Desc>". $list[$i]->Desc ."</Desc>";
$echo .= "<Picture>". $list[$i]->Pictures->Picture ."</Picture>";
$echo .= "<Quantity>". $list[$i]->Stocks->Stock->Quantity ."</Quantity>";        
}

$echo .= "</products>";

header("Content-Type: text/xml");
echo $echo;

我应该遵循什么方法?

我试图在此处使用“ str_replace”进行更改:属性名=“ SIZE”>这次,以下符号再次阻止我执行任何操作:“

0 个答案:

没有答案
相关问题