替换&;在PHP中修改XML节点
下面是一个示例XML结构:替换&;在PHP中修改XML节点,php,xml,simplexml,Php,Xml,Simplexml,下面是一个示例XML结构: <Products> <Product> <Id>1</Id> <Name>Product 1</Name> <Category>MEN</Category> <Category>Women</Category> <Product> <Produ
<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>
1.
产品1
男人
女人
2.
产品2
门2
女性2
我想要这样的文件:
<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>
1.
产品1
男:女
2.
产品2
男:女
所以基本上它会搜索产品中的节点。如果找到“Category”,它会将名称更改为“CategoryName”,并将所有子序列的Category节点值连接为一个由分号分隔的值
所以我写了这个小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;
}
}
}
?>
产品;
if($name->count())//检查它是否是“产品”节点
{
foreach($i项目)
{
$category=$i->category;
}
}
}
?>
有人能给我指一下正确的方向吗?我很少使用XML。试试这个:
$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();
$xml='1!'
1.
产品1
男人
女人
2.
产品2
门2
女性2
';
$dom=新的DOMDocument();
$dom->preserveWhiteSpace=false;
$dom->formatOutput=true;
$dom->loadXML($xml,LIBXML_NOBLANKS);
$xpath=newdomxpath($dom);
$ProductNode=$xpath->query(“//Product”);
如果($ProductNode->length){
foreach($ProductNode作为$node){
$category=$node->getElementsByTagName('category');
$str='';
//存储对节点的引用,
//以便以后可以删除它们
$del=array();
foreach(类别为$p){
$str.=$p->nodeValue.:';
$del[]=$p;
}
$str=trim($str,“:”);
$child=$dom->createElement('CategoryName',$str);
$node->appendChild($child);
foreach($del作为$p){
$p->parentNode->removeChild($p);
}
}
}
标题('content-type:text/xml');
echo$dom->saveXML();
希望有帮助。请用这个
<?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;
?>
Id.';
$table.=''.$item->Name';
$table.='';
$i=0;
foreach($item->类别为$cat)
{
如果($i>0){
$table.=':';
}
$table.=$cat;
$i++;
}
$table.='';
$table.='';
}
$table.='';
echo$表;
?>
如果要在遍历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());
$xml_obj=new DOMDocument();
$xml\u obj->loadXML($xml\u string,LIBXML\u NOBLANKS);
$xml_obj->preserveWhiteSpace=false;
$xml_obj->formatOutput=true;
$products=$xml_obj->getElementsByTagName(“产品”);
foreach($products as$product){
$cats=array();
$categories=$product->getElementsByTagName('categories');
$tot=$categories->length;
$to_delete=array();
对于($i=0;$i<$tot;$i++){
$cat=$categories->item($i);
$cats[]=$cat->textContent;
$to_delete[]=$cat;
}
foreach($to_delete作为$delete_节点){
$product->removeChild($delete_节点);
}
$product->appendChild($xml_obj->createElement('CategoryName',内爆(“:”,$cats));
}
打印($xml_obj->saveXML());
XML中的标记不匹配。您确定XML是正确的吗?抱歉,这是一个输入错误。我已经改正了。