嘿,我有一个xml文件,我需要做的是添加一个具有相同位置名称A的事件
<timetable>
<location name="A" >
<event >
<title>EVENT TITLE </title>
<subtitle>Amman -text </subtitle>
<description>Amman -text </description>
</event>
</location>
</timetable>
所以它可以像这样
<timetable>
<location name="A" >
<event >
<title>EVENT TITLE </title>
<subtitle>Amman -text </subtitle>
<description>Amman -text </description>
</event>
<event >
<title>EVENT TITLE </title>
<subtitle>Amman -text </subtitle>
<description>Amman -text </description>
</event>
</location>
</timetable>
我陷入了我的PHP代码,如果事件被创建,我想要解决,如果它创建了修改我的xml与事件的名称并添加新的
这是我的完整php插入代码`
if(isset($_GET['coname'])){
$coid = $_GET['id'];
$cname=$_GET['coname'];
$title = $_POST ['title'];
$sub = $_POST ['sub'];
$description = $_POST ['description'];
$location = $_POST ['location'];
$event = $_POST ['event'] ;
$str =$_POST ['str'] ;
$end =$_POST ['end'] ;
$topic = $_POST ['topic'] ;
$sql="INSERT INTO timeline (title,sub,description,location,event,str,end,topic,coid)
VALUES
('$title','$sub','$location','$location','$event','$str','$end','$topic','$coid')";
if (!mysqli_query($con,$sql))
{
die('Error: ' . mysqli_error($con));
}
echo "1 record added";
$q = mysqli_query($con,"SELECT * FROM timeline where coid = $coid") or die(mysqli_error());
$xml = '<timetable start="'.$st.'" end="'.$en.'" interval="'.$in.'" title="'.$da.'">';
while($r = mysqli_fetch_array($q)){
$loc=$r['topic'];
$evns=$r['str'];
$evne= $r['end'];
$xml .= '<location name="'.$loc.'" subtext=" ">';
$xml .= '<event start="'.$evns.'" end="'.$evne.'">';
$xml .= "<title>".$r['title']."</title>";
$xml .= "<subtitle>".$r['location']."</subtitle>";
$xml .= "<description>".$r['description']."</description>";
$xml .= "</event>";
$xml .= "</location>";
}
$xml .= "</timetable>";
$sxe = new SimpleXMLElement($xml);
$sxe->asXML('xml/'.$cname.'.xml'); `
答案 0 :(得分:1)
据我所知,你想创建一个子元素。这就是我要做的事情:
PHP:
$eleone=$_POST['elementone'];
$eletwo=$_POST['elementtwo'];
$file = "verbs.xml";
$openf = fopen($file, "c+") or die ("Cannot open file");
$str = fread ($openf, filesize($file));
$xml = new DOMDocument('1.0', 'iso-8859-1');
$xml->formatOutput=TRUE;
$xml->preserveWhiteSpace = FALSE;
$xml ->loadXML($openf) or die ("There has been an error with opening the XML file. Our team has been notified and will start working to fix this problem.");
//this is the original document
echo "<xmp>OLD:\n". $xml->saveXML(). "<xmp>";
//this is how you get the document element
$root= $xml ->documentElement;
$firstnode= $root->firstChild;
//good stuff right here; how to make a node
$ori= $firstnode->childNodes->item(2);
$eleadd= $xml->createElement($elementone);
$eleaddt= $xml->createTextNode($//what gets shown in $eleadd );
$eleadd->appendChild("$idt");
如果你不想要所有这些,你可以删除一些非关键的东西,比如父元素。如果您需要更多信息,http://www.phpeveryday.com/articles/PHP-XML-Adding-XML-Nodes-P414.html是您要去的地方,或者我找到我的信息的地方。
答案 1 :(得分:1)
Amer,我不是使用字符串创建XML,而是使用simplexml
方法:
在现有XML中插入新的<event>
:
$xml = simplexml_load_string($x); // assume XML in $x
$loc = $xml->xpath("location[@name = 'A']")[0]; // select <location> with name = A
$event = $loc->addChild("event");
$event->addAttribute("start", "2013-05-20 10:00:00");
$event->addAttribute("end", "2013-05-20 14:30:00");
$event->addChild("title", "some title");
$event->addChild("subtitle", "some subtitle");
$event->addChild("description", "some description");