XML与XML之间的转换;表单-PHP
我的代码适用于某种级别的xml,但我的逻辑需要修改,因为我希望它适用于每种类型的xml文件,我在xml中添加的任何元素都是可读的,并且可以编辑。请修改我的代码edit.php 这段代码正在读取xml文件并生成一个附加的表单XML与XML之间的转换;表单-PHP,php,xml,forms,Php,Xml,Forms,我的代码适用于某种级别的xml,但我的逻辑需要修改,因为我希望它适用于每种类型的xml文件,我在xml中添加的任何元素都是可读的,并且可以编辑。请修改我的代码edit.php 这段代码正在读取xml文件并生成一个附加的表单 <!DOCTYPE html> <html> <head> INSERT VALUES OF THE GIVEN PHRASE IDS :</head> <!-- <head> <link rel=
<!DOCTYPE html>
<html>
<head> INSERT VALUES OF THE GIVEN PHRASE IDS :</head>
<!-- <head> <link rel="stylesheet" type="text/css" href="style.css" /></head> -->
<body>
<form name="form" action="edit.php" method="post">
<?php
$file = <<<XML
<?xml version="1.0"?>
<?xml-stylesheet href="catalog.xsl" type="text/xsl"?>
<!DOCTYPE catalog SYSTEM "catalog.dtd">
<catalog>
<product description="Cardigan Sweater" id="123" value="" product_image="cardigan.jpg">
<catalog_item gender="Men's">
<size description="Medium">
<color_swatch image="red_cardigan.jpg" id="color" value="Red"/>
<color_swatch image="burgundy_cardigan.jpg" id="color" value="burgundy"/>
</size>
<size description="Large">
<color_swatch image="red_cardigan.jpg" id="color" value="Red"/>
<color_swatch image="burgundy_cardigan.jpg" id="color" value="burgundy"/>
</size>
</catalog_item>
</product>
</catalog>
XML;
/**
* @param SimpleXMLElement $node current node to process
* @param int $level Depth in the XML tree
* @param string $fieldPrefix
*/
function makeNodeField(SimpleXMLElement $node, $level, $fieldPrefix = "")
{
// Indentation with the node level
$indent = str_repeat(" ", $level);
echo $indent, $node->getName(), PHP_EOL;echo "<br/>";
$fieldName = $fieldPrefix;
// If there's an ID on the node, display it and show a text field
if (isset($node['id'])) {
echo $indent, strval($node['id']), ' ';
if (!empty($fieldPrefix)) {
$fieldName .= '__';
}
$fieldName .= $node->getName() . '__' . strval($node['id']);
printf('<input type="text" name="%s" value="%s" /><br/>', $fieldName, strval($node['value']));echo "<br/>";
echo PHP_EOL;
}
$count = 0;
foreach ($node as $childNode) {
// put a prefix in the children field name to get a valid field name for POST
$childPrefix = $fieldName;
if (!empty($fieldPrefix)) {
$childPrefix = $fieldName . '__' . $count++;
}
makeNodeField($childNode, $level + 1, $childPrefix);
}
}
$xml = simplexml_load_string($file);
makeNodeField($xml, 0);
?>
<input type='submit' name='submit'>
</form>
</body>
</html>
插入给定短语ID的值:
getName(),PHP_EOL;回声“
”;
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<gs-shop date="2015/4/23" version="10.0.3" id="gs" name="Garments_Shop">
<catalog>
<color_swatch image="red_cardigan.jpg" id="color" value="Red"/>
<color_swatch image="burgundy_cardigan.jpg" id="color" value="burgundy"/>
<color_swatch image="red_cardigan.jpg" id="color" value="Red"/>
<color_swatch image="burgundy_cardigan.jpg" id="color" value="burgundy"/>
</catalog>
</gs-shop>
how to modify this so it can work for this
<?xml-stylesheet href="catalog.xsl" type="text/xsl"?>
<!DOCTYPE catalog SYSTEM "catalog.dtd">
<catalog>
<product description="Cardigan Sweater" id="123" value="" product_image="cardigan.jpg">
<catalog_item gender="Men's">
<size description="Medium">
<color_swatch image="red_cardigan.jpg" id="color" value="blue"/>
<color_swatch image="burgundy_cardigan.jpg" id="color" value="blue"/>
</size>
<size description="Large">
<color_swatch image="red_cardigan.jpg" id="color" value="Blue"/>
<color_swatch image="burgundy_cardigan.jpg" id="color" value="blue"/>
</size>
</catalog_item>
</product>
</catalog>
and whatever i add to xml.
$fieldName=$fieldPrefix;
//如果节点上有ID,则显示该ID并显示文本字段
if(isset($node['id'])){
echo$indent,strval($node['id']),'';
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<gs-shop date="2015/4/23" version="10.0.3" id="gs" name="Garments_Shop">
<catalog>
<color_swatch image="red_cardigan.jpg" id="color" value="Red"/>
<color_swatch image="burgundy_cardigan.jpg" id="color" value="burgundy"/>
<color_swatch image="red_cardigan.jpg" id="color" value="Red"/>
<color_swatch image="burgundy_cardigan.jpg" id="color" value="burgundy"/>
</catalog>
</gs-shop>
how to modify this so it can work for this
<?xml-stylesheet href="catalog.xsl" type="text/xsl"?>
<!DOCTYPE catalog SYSTEM "catalog.dtd">
<catalog>
<product description="Cardigan Sweater" id="123" value="" product_image="cardigan.jpg">
<catalog_item gender="Men's">
<size description="Medium">
<color_swatch image="red_cardigan.jpg" id="color" value="blue"/>
<color_swatch image="burgundy_cardigan.jpg" id="color" value="blue"/>
</size>
<size description="Large">
<color_swatch image="red_cardigan.jpg" id="color" value="Blue"/>
<color_swatch image="burgundy_cardigan.jpg" id="color" value="blue"/>
</size>
</catalog_item>
</product>
</catalog>
and whatever i add to xml.
如果(!empty($fieldPrefix)){
$fieldName.='';
}
$fieldName.=$node->getName().'uuuuu'.strval($node['id']);
printf('
',$fieldName,strval($node['value']);echo“
”;
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<gs-shop date="2015/4/23" version="10.0.3" id="gs" name="Garments_Shop">
<catalog>
<color_swatch image="red_cardigan.jpg" id="color" value="Red"/>
<color_swatch image="burgundy_cardigan.jpg" id="color" value="burgundy"/>
<color_swatch image="red_cardigan.jpg" id="color" value="Red"/>
<color_swatch image="burgundy_cardigan.jpg" id="color" value="burgundy"/>
</catalog>
</gs-shop>
how to modify this so it can work for this
<?xml-stylesheet href="catalog.xsl" type="text/xsl"?>
<!DOCTYPE catalog SYSTEM "catalog.dtd">
<catalog>
<product description="Cardigan Sweater" id="123" value="" product_image="cardigan.jpg">
<catalog_item gender="Men's">
<size description="Medium">
<color_swatch image="red_cardigan.jpg" id="color" value="blue"/>
<color_swatch image="burgundy_cardigan.jpg" id="color" value="blue"/>
</size>
<size description="Large">
<color_swatch image="red_cardigan.jpg" id="color" value="Blue"/>
<color_swatch image="burgundy_cardigan.jpg" id="color" value="blue"/>
</size>
</catalog_item>
</product>
</catalog>
and whatever i add to xml.
echo PHP_EOL;
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<gs-shop date="2015/4/23" version="10.0.3" id="gs" name="Garments_Shop">
<catalog>
<color_swatch image="red_cardigan.jpg" id="color" value="Red"/>
<color_swatch image="burgundy_cardigan.jpg" id="color" value="burgundy"/>
<color_swatch image="red_cardigan.jpg" id="color" value="Red"/>
<color_swatch image="burgundy_cardigan.jpg" id="color" value="burgundy"/>
</catalog>
</gs-shop>
how to modify this so it can work for this
<?xml-stylesheet href="catalog.xsl" type="text/xsl"?>
<!DOCTYPE catalog SYSTEM "catalog.dtd">
<catalog>
<product description="Cardigan Sweater" id="123" value="" product_image="cardigan.jpg">
<catalog_item gender="Men's">
<size description="Medium">
<color_swatch image="red_cardigan.jpg" id="color" value="blue"/>
<color_swatch image="burgundy_cardigan.jpg" id="color" value="blue"/>
</size>
<size description="Large">
<color_swatch image="red_cardigan.jpg" id="color" value="Blue"/>
<color_swatch image="burgundy_cardigan.jpg" id="color" value="blue"/>
</size>
</catalog_item>
</product>
</catalog>
and whatever i add to xml.
}
$count=0;
foreach($node作为$childNode){
//在子字段名中添加前缀以获取POST的有效字段名
$childPrefix=$fieldName;
如果(!empty($fieldPrefix)){
$childPrefix=$fieldName.'.\'$count++;
}
makeNodeField($childNode,$level+1,$childPrefix);
}
}
$xml=simplexml\u load\u字符串($file);
makeNodeField($xml,0);
?>
它会创建这样的表单(附加快照)
edit.php()
formatOutput=true;
$last_index_id=array();
$i_id=1;
foreach($key=>$value的数组)
{
$key=$key;
$attrvalue=$value;
//拆分唯一的输入字段名称
if($array[$key]==“”| | ctype_space($array[$key])){
$array[$key]=null;
}
$expkey=explode(“\uuuuuuuuuuuuuuuuuuuuuuuuuuuuuuu,$key);
回声“
”;
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<gs-shop date="2015/4/23" version="10.0.3" id="gs" name="Garments_Shop">
<catalog>
<color_swatch image="red_cardigan.jpg" id="color" value="Red"/>
<color_swatch image="burgundy_cardigan.jpg" id="color" value="burgundy"/>
<color_swatch image="red_cardigan.jpg" id="color" value="Red"/>
<color_swatch image="burgundy_cardigan.jpg" id="color" value="burgundy"/>
</catalog>
</gs-shop>
how to modify this so it can work for this
<?xml-stylesheet href="catalog.xsl" type="text/xsl"?>
<!DOCTYPE catalog SYSTEM "catalog.dtd">
<catalog>
<product description="Cardigan Sweater" id="123" value="" product_image="cardigan.jpg">
<catalog_item gender="Men's">
<size description="Medium">
<color_swatch image="red_cardigan.jpg" id="color" value="blue"/>
<color_swatch image="burgundy_cardigan.jpg" id="color" value="blue"/>
</size>
<size description="Large">
<color_swatch image="red_cardigan.jpg" id="color" value="Blue"/>
<color_swatch image="burgundy_cardigan.jpg" id="color" value="blue"/>
</size>
</catalog_item>
</product>
</catalog>
and whatever i add to xml.
//词组ID
数组推送($last_index_id,end($expkey));
echo“”;print_r($expkey);echo“”;
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<gs-shop date="2015/4/23" version="10.0.3" id="gs" name="Garments_Shop">
<catalog>
<color_swatch image="red_cardigan.jpg" id="color" value="Red"/>
<color_swatch image="burgundy_cardigan.jpg" id="color" value="burgundy"/>
<color_swatch image="red_cardigan.jpg" id="color" value="Red"/>
<color_swatch image="burgundy_cardigan.jpg" id="color" value="burgundy"/>
</catalog>
</gs-shop>
how to modify this so it can work for this
<?xml-stylesheet href="catalog.xsl" type="text/xsl"?>
<!DOCTYPE catalog SYSTEM "catalog.dtd">
<catalog>
<product description="Cardigan Sweater" id="123" value="" product_image="cardigan.jpg">
<catalog_item gender="Men's">
<size description="Medium">
<color_swatch image="red_cardigan.jpg" id="color" value="blue"/>
<color_swatch image="burgundy_cardigan.jpg" id="color" value="blue"/>
</size>
<size description="Large">
<color_swatch image="red_cardigan.jpg" id="color" value="Blue"/>
<color_swatch image="burgundy_cardigan.jpg" id="color" value="blue"/>
</size>
</catalog_item>
</product>
</catalog>
and whatever i add to xml.
$first_key=($expkey[0]);
//第一根元素
如果($xml->getElementsByTagName($first\u key)->length==0&&$first\u key!='submit'){
$xml\u element=$xml->createElement($first\u key,“”);
$xml->appendChild($xml\u元素);
}
foreach($expkey作为$key=>$value){
如果(是数值($value)){
$secondtag=$expkey[$key+1];
//第二标签
$xml_标记=$xml->createElement($secondtag,“”);
//属性
$domAttribute=$xml->createAttribute('id');
$domAttribute->value=$last_index_id[$i_id];
$domAttribute1=$xml->createAttribute('value');
$domAttribute1->value=$attrvalue;
$xml_tag->appendChild($domAttribute);
$xml_tag->appendChild($domAttribute1);
$xml\u element->appendChild($xml\u标记);
$i_id++;
}
}
}
$xml->save(“rozeena.xml”);
}
printxml($\u POST);
}退出();
?>
现在这个edit.php正在为这个xml文件工作
如何对此进行修改,使其能够为此工作
以及我添加到xml中的任何内容。
例如:
注意事项消息已经说明:
字段名
未定义。这意味着,您使用了fieldName
,但尚未定义它。必须首先指定一个值
根据要处理的字段,该值必须是输出中显示的值之一,例如
product_123
product\u 123\u 0\u 0\u color\u swatch\u color
product\u 123\u 0\u 1\u color\u swatch\u color
product\u 123\u 1\u 0\u color\u swatch\u color
product\u 123\u 1\u 1\u color\u swatch\u color
要从输入字段还原XML,必须解析并拆分
$\u POST
变量中的索引值。虽然从您所展示的内容来看,这是不可能的,因为缺少
,
或
。@vipul sharma你能帮忙吗?@Sourabh你能帮忙吗?@scollado你能帮忙吗?@Drakes你能帮忙吗?@Padawan00123你能帮忙吗?那么我应该在字段名中给出什么。我解释了我想做什么。我只想打印完全相同的XML,但无论我在输入字段中给出什么,都要使用更新的值。我是一个初学者,请帮助我。我不知道如何实施。谢谢你从所有这些属性我只想要Id和值。忽略性别、描述。我必须更改相应ID的值