我试图找到一种方法来保存编辑后的XML文件,而不包括声明。但我需要能够设置一个地址。因为我正在保存原始XML,并保存在临时位置之一(原件的副本用于访问javascript,因为原始位于本地文件中)。
所以我尝试了$ dom-> saveXML($ xml-> documentElement);但是出现了一些保存错误。 这是获取表单数据并将其保存到当前加载的xml的php页面,然后保存它和另一个副本(在下面的代码底部)
<?php
//header('Location: ../index.php' );
$dom = new DOMDocument();
$dom->preserveWhiteSpace = false;
$dom->formatOutput = true;
$dom->load("../data/fileloc.xml");
$fileUrlTag = $dom->getElementsByTagName('fileurl')->item(0);
$fileName = $fileUrlTag->getAttribute('filename');
$fileAddress = $fileUrlTag->getAttribute('address');
$fileUrl = $fileAddress.$fileName;
if(isset($_REQUEST['ok'])){
$xml = new DOMDocument();
$xml->preserveWhiteSpace = false;
$xml->formatOutput = true;
$xml->load($fileUrl);
//checks if Objects tag exists
$rootTag = $xml->getElementsByTagName("Objects")->item(0);
//if Objects tag doesnt exist, creates it and a unique id
if($xml->getElementsByTagName("Objects")->length == 0){
$rootTag = $xml->createElement("Objects");
$alph = "0123456789ABCDEF";
$ranStr = '';
for($i=0;$i<32;$i++){
if($i==8 || $i==12 || $i==16 || $i==20){
$ranStr .= "-";
}
$pos = rand(0,35);
$ranStr .= $alph[rand(0, strlen($alph)-1)];
};
$randId = "{".$ranStr."}";
$rootTag->setAttribute("OverlayId",$randId);
$objDocTag = $xml->getElementsByTagName("ObjectsDoc")->item(0);
if($objDocTag->getAttribute("Version")->length == 0){
$objDocTag->setAttribute("Version","1.0");
};
$objDocTag->appendChild($rootTag);
};
//used to set ID on Layer tag - gets value from the Objects->OverlayId attribute
$getId = $rootTag->getAttribute('OverlayId');
//used to set ID on Object tag
$numObj = $xml->getElementsByTagName('Object')->length;
//commstag determines colours for Pen and Brush tags
$commsTag = $xml->createElement("comms",$_REQUEST["comms"]);
//convert from deg to radians
$lat = $_REQUEST['lat'];
$long = $_REQUEST['long'];
$latRad = ($lat*6.28318)/360;
$longRad = ($long*6.28318)/360;
if($_REQUEST['comms']=='Good'){
$color = array(255,0,255,0);
};
if($_REQUEST['comms']=='Bad'){
$color = array(255,0,255,255);
};
if($_REQUEST['comms']=='None'){
$color = array(255,0,0,255);
};
//Create object and set object attributes
$objectTag = $xml->createElement("Object");
$objectTag->setAttribute("ID",($numObj+1000));
$objectTag->setAttribute("Parent",$_REQUEST['level']);
$objectTag->setAttribute("Visibile","1");
$objectTag->setAttribute("type","MAPDRAW_OBJECT");
$graphicTag = $xml->createElement("Graphic");
$graphicTag->setAttribute('AlwaysShowName','1');
$graphicTag->setAttribute('Font',"Calibri");
$graphicTag->setAttribute('Name',strtoupper($_REQUEST["callsign"]));
$graphicTag->setAttribute('TextColor',"0");
$graphicTag->setAttribute('TextPosition',6);
$graphicTag->setAttribute('Version',"1.0");
$graphicTag->setAttribute('Visible',1);
$graphicTag->setAttribute('Size',12);
$layerTag = $xml->createElement("Layer");
$layerTag->setAttribute('ID',$getId);
$graphicPrimTag = $xml->createElement("GraphicPrimitive");
$graphicPrimTag->setAttribute('Type','CircleSector');
$graphicPrimTag->setAttribute('Version','1.0');
$penTag = $xml->createElement('Pen');
$penTag->setAttribute('A',255);
$penTag->setAttribute('B',255);
$penTag->setAttribute('G',255);
$penTag->setAttribute('R',255);
$penTag->setAttribute('Type',0);
$penTag->setAttribute('size',4);
$brushTag = $xml->createElement('Brush');
$brushTag->setAttribute('A',$color[0]);
$brushTag->setAttribute('B',$color[1]);
$brushTag->setAttribute('FillStyle',10);
$brushTag->setAttribute('G',$color[2]);
$brushTag->setAttribute('R',$color[3]);
$circleTag = $xml->createElement('CircleSector');
$circleTag->setAttribute('Radius',500);
$fontTag = $xml->createElement('Font');
$fontTag->setAttribute('Name','Calibri');
$fontTag->setAttribute('size','15');
$coordsTag = $xml->createElement("Coordinates");
$coordsTag->setAttribute("Absolute","1");
$coordsTag->setAttribute('System','WGS84');
$refcoordTag = $xml->createElement("RefCoordinate");
$refcoordTag->setAttribute('Rotation',0);
$refcoordTag->setAttribute('X',$latRad);
$refcoordTag->setAttribute('Y',$longRad);
$refcoordTag->setAttribute('Z','0');
$coordTag = $xml->createElement("Coordinate");
$coordTag->setAttribute('X',$latRad);
$coordTag->setAttribute('Y',$longRad);
$coordTag->setAttribute('Z','0');
$coordTag->setAttribute('index','0');
$accessTag = $xml->createElement("AccessRights");
$accessTag->setAttribute('Editable',0);
$accessTag->setAttribute('Moveable',0);
$accessTag->setAttribute('Selectable',1);
//Append RefCoordinate and Coordinate to Coordinates
$coordsTag->appendChild($refcoordTag);
$coordsTag->appendChild($coordTag);
//append Pen, Brush, CircleSelector and Font to GraphicPrimitive
$graphicPrimTag->appendChild($penTag);
$graphicPrimTag->appendChild($brushTag);
$graphicPrimTag->appendChild($circleTag);
$graphicPrimTag->appendChild($fontTag);
//Append Layer, GraphicPrimitive, Coordinates to Graphic
$graphicTag->appendChild($layerTag);
$graphicTag->appendChild($graphicPrimTag);
$graphicTag->appendChild($coordsTag);
//Append Graphic to Object
$objectTag->appendChild($graphicTag);
$objectTag->appendChild($accessTag);
//Append Object to Objects
$rootTag->appendChild($objectTag);
echo($fileUrl);
$xml->save($fileUrl);
$xml->save("..temp/".$fileName);
exit();
};
我尝试运行这些(第一个保存到我假设的$ xml加载的位置?第二个到javascript的临时文件夹 - 第二个可以有声明)。
$xml->saveXML($xml->documentElement);
$xml->save("..temp/".$fileName);
但是下面会出现这个错误
Warning: DOMDocument::save(..temp/bms_overlays.xml) [domdocument.save]: failed to open stream: No such file or directory in D:\Programs\server2go\htdocs\analyst\scripts\createcontent.php on line 152
任何帮助都会非常感激,因为这是我第一次玩php和xml。
干杯,
米切尔