PHP查找并修改含POST传入代码的XML文件报错求助
Fixing Undefined Offset and Non-Object Errors in Your XML Parsing Code
Let's break down why you're seeing those errors and how to fix them:
Root Causes of the Errors
- Undefined Offset: 3: Your XPath query
$xml->xpath("pedido")returns an array of all<pedido>elements in the XML file. In your example XML, there's only one<pedido>element (so the array has index 0, not 3). Trying to access index 3 when it doesn't exist throws this error. - Trying to get property of non-object: When
$result[3]doesn't exist, it's not a SimpleXMLElement object—so accessing->numpedidoon a non-object triggers this second error.
Corrected Code
Here's a revised version of your code that addresses these issues and makes your logic more robust:
// Ensure $dilma is set with the value you're searching for (e.g., from POST) $dilma = $_POST['numpedido'] ?? ''; // Use null coalescing to avoid undefined variable errors // Scan the xml directory $files = scandir('xml/.'); foreach ($files as $file) { // Skip directory markers if ($file === '.' || $file === '..') continue; $filePath = "xml/" . $file; // Load the XML file and check if it loaded successfully $xml = simplexml_load_file($filePath); if (!$xml) { echo "Failed to load XML file: $filePath<br>"; continue; } // Get all pedido elements $pedidos = $xml->xpath("pedido"); // Check if any pedido elements were found if (empty($pedidos)) { echo "No pedido elements found in $filePath<br>"; continue; } // Loop through each pedido to find the matching numpedido foreach ($pedidos as $pedido) { // Compare the numpedido value (cast to string to avoid type issues) if ((string)$pedido->numpedido === (string)$dilma) { echo "Found matching pedido in $filePath!<br>"; // Here you can perform your modification logic // Example: Update the status $pedido->status = 'Modificado'; // Save the changes back to the XML file $xml->asXML($filePath); break; // Exit the loop once found if you only need the first match } } }
Key Improvements
- No hardcoded indexes: Instead of assuming index 3 exists, we loop through all
<pedido>elements to find the match. - XML validation: We check if
simplexml_load_filesucceeded (in case the XML file is corrupted or invalid). - Safe variable handling: We use
$_POST['numpedido'] ?? ''to ensure$dilmais always defined (even if the POST parameter is missing). - Type-safe comparison: Casting both values to strings ensures we're comparing content correctly (SimpleXMLElement objects can behave unexpectedly in direct comparisons).
- Modification & saving: Added example code to modify the XML and save the changes back to the file (aligning with your goal of updating the XML).
Notes About Your XML Structure
In your example XML, there's only one <pedido> element. If your actual files have multiple <pedido> elements nested under a root node (like <pedidos>), adjust your XPath query to $xml->xpath("//pedido") to find all <pedido> elements anywhere in the document.
内容的提问来源于stack exchange,提问作者João Pedro
相关产品推荐
相关产品推荐

