Deleting XML Node

I’m having trouble deleting an xml node. I’m getting not found error. There seems to be a match, in the foreach loop when it matches the attribute name, I get data. I’m not sure how to go about this.

	$networksDocXML = file_get_contents($networksXML);
						
									if (trim($networksDocXML) == '') {
										
										$networksDocXML = file_get_contents($networksXML);
										
											if (trim($networksDocXML) == '') {
												
												$returnMessage['xmlError'] = 'empty document';
												die(json_encode($returnMessage));
												
											}
									
										
									}

									$networkDoc = simplexml_load_string($networksDocXML);
									$networkName = $data['network'];
									$networks = $networkDoc->xpath("//networks/user[@token='$token']/networks/network[@name='$networkName']");
									$userToken = $_SESSION['token'];
									
									if(!empty($networks))
									{
										
								
										$dom = new DOMDocument;
										$dom->preserveWhiteSpace = FALSE;
										$dom->loadXML($networkDoc->asXML());
										$dom->formatOutput = TRUE;
										
										$xpath = new DOMXpath($dom);
                                        
										$list = $xpath->query("//networks/user[@token='$userToken']/networks/network[@name='$networkName']");
																					
										$nodeToRemove = null;
										foreach ($list as $domElement){
										  $attrValue = $domElement->getAttribute('name');
										  if ($attrValue == $networkName) {
											$nodeToRemove = $domElement; 
											
										  }
										}
										
										if(!empty($list))
										{
											$dom->removeChild($nodeToRemove);
										}
										
										if(is_file($networksXML))
										{
												
											file_put_contents($networksXML, $dom->saveXML());
												
										}
										
										$returnMessage['message'] = 'network deleted';
										$returnMessage['network'] = $data['network'];
												

So which line is throwing the error, and what specific error? I’m not inclined to go deep diving on your code if the problem is you’ve typoed your XML file’s filepath…

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.