You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

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

  1. 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.
  2. Trying to get property of non-object: When $result[3] doesn't exist, it's not a SimpleXMLElement object—so accessing ->numpedido on 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_file succeeded (in case the XML file is corrupted or invalid).
  • Safe variable handling: We use $_POST['numpedido'] ?? '' to ensure $dilma is 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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.29 06:52:28