问题
I am trying to find a way to save the XML file once edited, without including the declaration. But i need to be able to set an address. As i am saving over the original XML, and saving over a temp location one (duplicate of the original for javascript to access, as the original is in a local file).
So i tried the $dom->saveXML($xml->documentElement); but comes out with some save errors. This is the php page that gets the form data and saves it to the currently loaded xml, then saves it and another copy (Down the bottom of the below code)
<?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();
};
I tried running these (first one to save to location that $xml was loaded from i assume? and the second one to a temp folder for javascript - second one can have the declaration).
$xml->saveXML($xml->documentElement);
$xml->save("..temp/".$fileName);
But get this error below
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
Any help would be Greatly appreciated as this is the first time i have played with php and xml.
Cheers,
Mitchell
来源:https://stackoverflow.com/questions/45789626/save-xml-file-without-declaration-with-specific-address