0

这是一个示例 XML 结构:

<Products>
    <Product>
        <Id>1</Id>
        <Name>Product 1</Name>
        <Category>MEN</Category>
        <Category>Women</Category>
    <Product>
    <Product>
        <Id>2</Id>
        <Name>Product 2</Name>
        <Category>MEN2</Category>
        <Category>Women2</Category>
    <Product>
</Products>

我想要这样的文件:

<Products>
    <Product>
        <Id>1</Id>
        <Name>Product 1</Name>
        <CategoryName>MEN:Women</CategoryName>
    <Product>
    <Product>
        <Id>2</Id>
        <Name>Product 2</Name>
        <CategoryName>MEN:Women</CategoryName>
    <Product>
</Products>

所以基本上它会搜索产品中的节点。如果找到“Category”,它会将名称更改为“CategoryName”,并将所有子后续类别节点值连接成一个以分号分隔的单个值。

所以我写了这个小PHP,但不知道如何让它工作。

<?php
    $xmlFile = "test.xml" //assume the contents are in the file
    $xml = simplexml_load_file($xmlFile);

    foreach($xml as $item)
    {
        $name = $item->Product;
        if($name->count()) //check if its a "product" node
        {
            foreach($item as $i)
            {
                $category = $i->Category;
            }
        }
     }
?> 

有人可以指出我正确的方向吗?我没有太多使用 XML。

4

3 回答 3

1

请使用这个

<?php
    $xmlFile = "test.xml"; //assume the contents are in the file
    $xml = simplexml_load_file($xmlFile);   
    $table = '<Products>';
    foreach($xml as $item)
    {    
      $table .= '<Product>';
      $table .= '<Id>'.$item->Id.'</Id>';
      $table .= '<Name>'.$item->Name.'</Name>';
      $table .= '<Category>';
      $i = 0;
      foreach($item->Category as $cat)
        {
         if($i>0){
         $table .= ':'; 
         }   
         $table .= $cat;
         $i++;
        }
       $table .= '</Category>';
       $table .= '</Product>'; 
     }
      $table .= '</Products>';
      echo $table;
?> 
于 2013-07-26T06:05:32.663 回答
0

尝试这个:

$xml = '<Products>
    <Product>
        <Id>1</Id>
        <Name>Product 1</Name>
        <Category>MEN</Category>
        <Category>Women</Category>
    </Product>
    <Product>
        <Id>2</Id>
        <Name>Product 2</Name>
        <Category>MEN2</Category>
        <Category>Women2</Category>
    </Product>
</Products>';

$dom = new DOMDocument();
$dom->preserveWhiteSpace = false;
$dom->formatOutput = true;
$dom->loadXML( $xml, LIBXML_NOBLANKS );

$xpath = new DOMXPath( $dom );

$ProductNode = $xpath->query( "//Product" );

if( $ProductNode->length ) {
    foreach ( $ProductNode as $node ) {
        $category = $node->getElementsByTagName( 'Category' );
        $str = '';
            // store a reference to the nodes,
            // so that they can be deleted later
        $del = array();

        foreach ( $category as $p ) {
            $str .= $p->nodeValue . ':';
            $del[] = $p;
        }

        $str = trim( $str, ':' );

        $child = $dom->createElement( 'CategoryName', $str );
        $node->appendChild( $child );

        foreach ( $del as $p ) {
            $p->parentNode->removeChild( $p );
        }
    }
}

header('content-type: text/xml');
echo $dom->saveXML();

希望能帮助到你。

于 2013-07-26T05:59:21.190 回答
0

如果您想在遍历 XML 时修改它,请使用 DomDocument:

$xml_obj = new DOMDocument();
$xml_obj->loadXML($xml_string, LIBXML_NOBLANKS );
$xml_obj->preserveWhiteSpace = false;
$xml_obj->formatOutput = true;
$products = $xml_obj->getElementsByTagName('Product');
foreach ($products as $product) {
    $cats = array();
    $categories = $product->getElementsByTagName('Category');
    $tot = $categories->length;
    $to_delete = array();
    for($i = 0; $i < $tot;$i++) {
        $cat = $categories->item($i);
        $cats[] = $cat->textContent;
        $to_delete[]  = $cat;

    }
    foreach ($to_delete as $delete_node) {
        $product->removeChild($delete_node);
    }
    $product->appendChild($xml_obj->createElement('CategoryName', implode(":", $cats)));
}

print ($xml_obj->saveXML());
于 2013-07-26T06:15:21.253 回答