【发布时间】:2013-01-12 03:38:16
【问题描述】:
我遇到了 DOMDocument 和空格的问题。目前我有两种类型的 XML 文件。大约一年前手动创建了一个文件,我将这个文件称为 A。第二个文件,文件 B,是使用 PHP DOMDocument 生成的。我一直在努力(未成功)使文件 A 中的空格与文件 B 匹配。
这是它的工作原理... 用户可以选择将新的<Slide> 元素添加到 XML 文件中。添加新幻灯片后,用户可以选择将新的<Items> 作为<Slide> 元素的子元素添加到XML 文件中。
当我将 <Slide> 元素添加到文件 B 时,它就像一个魅力。我什至可以添加一个零问题的新<Item> 元素。但是,当我尝试访问新的 <Identifier> 元素时,我刚刚使用下面的第二个 PHP 脚本和 $order != 'remove' 添加到文件 B 中,我错过了一个节点,而是选择了 <Information/>。
似乎手动创建的文件 A 有我生成的文件 B 中不存在的空白。我尝试了 preserveWhitespace 属性,但没有帮助。
对我如何解决这个问题有什么建议吗?建设性的批评也很受欢迎,因为这是我对动态 XML 操作的第一次尝试。很抱歉,感谢您的时间!
文件 A - 手动创建 - 我正在尝试匹配此文件!
<?xml version="1.0" encoding="UTF-8"?>
<root>
<Areas>Head & Neck</Areas>
<Area>Head & Neck</Area>
<Type>Angiograph</Type>
<Slide>Ag-01a
<Title>Catheter Angiography</Title>
<Item1>
<Identifier interestCoord=".51,.73" locator="point" labelBool="true" labelTxt="" leaderBool="true">Aortic Arch
</Identifier>
<Information/>
<Question A="" B="" C="" D="" E="" Answer=""/>
</Item1>
.... More Items
文件 B - 在用户添加 <Slide> 之前。这部分是手动创建的。一个模板,如果你愿意的话。用户输入幻灯片名称后,使用下面的代码块生成新幻灯片。
<?xml version="1.0" encoding="UTF-8"?>
<root>
<Areas>Head & Neck</Areas>
<Area>Head & Neck</Area>
<Type>Brain Sections</Type>
</root>
文件 B - 在用户添加新的 <Slide> 和 <Item> 之后。显示的格式表示由 DOMDocument 创建的格式。 我认为这是发生错误的地方!空格!!!
<Slide>Ag-09a
<Title>Catheter Angiography</Title>
<Item1><Identifier locator="point" interestCoord="0.143,0.65" labelBool="true" labelTxt="" leaderBool="false">Orbit</Identifier><Information/><Question A="" B="" C="" D="" E="" Answer=""/></Item1></Slide>
用于向 XML 添加新的 <Slide> 元素的 PHP 脚本
<?php
session_start();
//Constants
$SECTION_SEP = "========================================================================</br>";
//Variables used to construct file path
$area = trim($_POST['area']);
$slideType = trim($_POST['slideType']);
$rawSlides = trim($_POST['theseSlides']);
$newSlideList = explode(",", $rawSlides);
$fileLocation = "../XML/".$area."/".$slideType."/".$area.".XML";
$dom = new DOMDocument();
echo('New DOMDocument created!</br>');
$dom->load($fileLocation);
echo('XML file loaded!</br>');
/*$dom->preserveWhiteSpace = false;
echo('White space removed!</br>');*/
$dom->documentElement;
echo('DOM initialized!</br>');
if ($dom->getElementsByTagName('Slide')->length == 0){ //New file with no slides
foreach ($newSlideList as $slide){
$newSlide = $dom->createElement('Slide', $slide);
$newTitle = $dom->createElement('Title', 'Scan');
//Add the title element to the Item
$newSlide->appendChild($newTitle);
$dom->childNodes->item(0)->appendChild($newSlide);
echo($slide." has been added to the list!</br>");
}
} else {
$locators = $dom->getElementsByTagName('Slide');
}
if($dom->save($fileLocation)){
echo("File saved successfully!!");
}else echo("There was a problem saving the file!");
用于添加/编辑/删除 <Item> 和 <Identifier> 节点的 PHP 脚本取决于 $orders 的值 == 警告!冗长:/
<?php
session_start();
//Constants
$SECTION_SEP = "========================================================================</br>";
//Variables used to construct file path
$area = trim($_POST['area']);
$slideType = trim($_POST['slideType']);
$fileLocation = "../XML/".$area."/".$slideType."/".$area.".XML";
//echo("File location:".$fileLocation);
//Current data (c_ for current)
$c_poi = "";
$c_type = "";
$c_lblBool = "";
$c_lblOverride = "";
$c_leaderBool = "";
//Determine if this visit is for new or old data
$orders = trim($_POST['orders']);
//Variables used to replace information in XML file loaded below (n_ for new)
$n_slideName = trim($_POST['slideName']); //slide name in view format ie Ag-01a
$n_identName = trim($_POST['ident']); //contains multiple information separated by comma ie 0,Aortic Arch
$n_type = trim($_POST['type']); //locator type
$n_poi = trim($_POST['poi']);
$n_lblBool = trim($_POST['lblBool']);
$n_lblOverride = trim($_POST['lblOverride']);
echo("Modified: ".date('c')."</br>");
$dom = new DOMDocument();
echo('New DOMDocument created!</br>');
$dom->load($fileLocation);
echo('XML file loaded!</br>');
/*$dom->preserveWhiteSpace = false;
echo('White space removed!</br>');*/
$dom->documentElement;
echo('DOM initialized!</br>');
$locators = $dom->getElementsByTagName('Slide');
echo($locators->length.' elements retrieved</br>');
$slideEntryFound = false;
$identEntryFound = false;
$identAttributesFound = false;
echo($SECTION_SEP);
//Locate the correct slide node
foreach ($locators as $locator){
//If there is a match, store the infomation
// rawSlide[x].childNode[0].nodeValue
if(strcmp(trim($locator->childNodes->item(0)->nodeValue),$n_slideName) == 0){
$slideEntryFound = true;
$slideChildren = $locator->childNodes;
//Locate the correct identifier node
foreach($slideChildren as $child){
if( strcmp(trim($child->nodeValue), substr($n_identName,strpos($n_identName,",")+1)) == 0){
$identEntryFound = true;
if (strcmp($orders, "remove") == 0){//Removing an element
echo("The identifier being removed is: ".trim($child->nodeValue."</br>"));
echo("The node path is: ".($child->childNodes->item(1)->getNodePath())."</br>");
echo($SECTION_SEP);
$locator->removeChild($child);
echo("Identifier successfully removed!</br>");
echo($SECTION_SEP);
break;
} else {//Not removing anything - Adding or Editing
echo("The identifier being modified is: ".trim($child->nodeValue."</br>"));
echo("The node path is: ".($child->childNodes->item(1)->getNodePath())."</br>");
echo($SECTION_SEP);
if($child->childNodes->item(1)->hasAttributes()){
$identAttributesFound = true;
$c_poi = $child->childNodes->item(1)->getAttribute('interestCoord');
echo("--Current interestCoord: ".$c_poi."</br>");
echo("++New interestCoord: ".$n_poi."</br>");
if(strcmp($c_poi, $n_poi) != 0){
$child->childNodes->item(1)->setAttribute('interestCoord',$n_poi);
}
$c_type = $child->childNodes->item(1)->getAttribute('locator');
echo("--Current locator: ".$c_type."</br>");
echo("++New locator: ".$n_type."</br>");
$c_lblBool = $child->childNodes->item(1)->getAttribute('labelBool');
echo("--Current labelBool: ".$c_lblBool."</br>");
//echo("++New labelBool: ".$n_lblBool."</br>");
$c_lblOverride = $child->childNodes->item(1)->getAttribute('labelTxt');
echo("--Current labelOverride: ".$c_lblOverride."</br>");
echo("++New labelOverride: ".$n_lblOverride."</br>");
$c_leaderBool = $child->childNodes->item(1)->getAttribute('leaderBool');
echo("--Current leaderBool: ".$c_leaderBool."</br>");
//echo("++New leaderBool: ".$n_leaderBool."</br>");
if($n_lblOverride != ""){
echo("**A new label override was detected. The identifier will have the alias ".$n_lblOverride.".");
}
break;
} else echo("Fatal Error - Node does not contain attributes!</br>");
if($identEntryFound == true && $identAttributesFound == false)
echo("Error - Attribute entry not found!");
break;
}
}
}
if($slideEntryFound == true && $identEntryFound == false && $orders != "remove"){
echo("The identifier was not found... creating a new identifier!</br>");
//Create a new Element
$newElement = $dom->createElement("Item".((integer)(substr($n_identName,0,strpos($n_identName,",")))+1));
echo("New element created!!</br>");
//Create new Item children
$newSubElem = $dom->createElement("Identifier", substr($n_identName,strpos($n_identName,",")+1));
$newSubElem->setAttribute('locator',$n_type);
$newSubElem ->setAttribute('interestCoord',$n_poi);
$newSubElem->setAttribute('labelBool', $n_lblBool);
$newSubElem->setAttribute('labelTxt', $n_lblOverride);
//TODO link this next one to a variable instead of hard coding
$newSubElem->setAttribute('leaderBool', "false");
//Info Child
$newInfoElem = $dom->createElement("Information");
//Question Child
$newQuestion = $dom->createElement("Question");
$newQuestion->setAttribute('A', "");
$newQuestion->setAttribute('B', "");
$newQuestion->setAttribute('C', "");
$newQuestion->setAttribute('D', "");
$newQuestion->setAttribute('E', "");
$newQuestion->setAttribute('Answer', "");
//Add new children to main Item
$newElement->appendChild($newSubElem);
$newElement->appendChild($newInfoElem);
$newElement->appendChild($newQuestion);
$locator->appendChild($newElement);
echo("New identifier added!!</br>");
break;
}
} else {
}
}
if($slideEntryFound == false)
echo("Error - Slide entry not found!");
if($dom->save($fileLocation)){
echo("File saved successfully!!");
echo('<div id="phpHandleBtns>"></br><form><button type="submit" id="continueEdit" formaction="../edit.php">Continue Editing</button>'.
'</br><button type="submit" id="doneEdit" formaction="../main.php">Done Editing</button></form></div>');
}else echo("There was a problem saving the file!");
?>
【问题讨论】:
-
一个问题:为什么? (是的,您可以创建空白元素,这些是 DOMText 节点 - 请参阅 php.net/domdocument.createtextnode)编辑: 另请参阅 PHP XML how to output nice format 和 Converting indentation with preg_replace (no callback)。
-
@hakre 好问题。我已经以“旧”格式创建了大量这些元素。需要很长时间才能返回并以“新”格式重新创建模块。我认为将新的调整为与旧的一起使用会更容易。我会看看你发布的信息。感谢您的意见!
-
我仍然不明白差异会如何导致问题,只要这只是空格,您通常不需要关心 XML。
-
@hakre 我也不明白。当我尝试访问我手动(使用记事本)创建的 xml 文件时,它会找到正确的值。当我尝试访问位于使用 XMLDocument 创建的文件中的同一节点时,它会关闭一个子节点。我使用相同的代码访问两个 XML 文件,这告诉我 xml 文件不同。
标签: php javascript xml xmldocument