Hi,

I have a PHP file that gets an XML files from a HTML form. The PHP files needs to generate a new HTML file containing a table with the the data retrived from the XML file. I was able to this this properly (using expat). However, before adding the data to the table, I would need to sort the data based on one of the XML elements in my file. This is where my problem begins. I'm trying to build an array from by XML file using the following code:

 $xmlFile = $_GET["xmlFile"];
 $p = xml_parser_create();
 xml_parse_into_struct($p, $xmlFile, $vals, $index);
 xml_parser_free($p);
 echo "First array <br>";
 print_r($vals);
 echo "<br>Second Array<br>";
 print_r($index);

However, both $vals and $index are empty! Any suggestions on what is wrong and how to fix it? I'm fairly new to PHP.

Dani AI

Generated

Likely cause and quick fix
The parser functions expect actual XML text, not a filename or upload container. As suggested, confirm whether $_GET['xmlFile'] contains the XML document itself or just a filename/URL. If your form uploaded a file you must read it from $_FILES (and the form must use method="post" and enctype="multipart/form-data"). If you only have a path/URL, read that file into a string first (for example with file_get_contents) and then parse the string.

Parsing + sorting (example)
A straightforward approach is: load XML into a SimpleXMLElement, build an array of rows where each row includes the element you want to sort by, then use usort() (or array_multisort()) to order the rows before rendering the HTML table. Example pattern:

$xmlString = file_get_contents($sourcePathOrUploadedTmpName);
$xml = simplexml_load_string($xmlString, 'SimpleXMLElement', LIBXML_NOCDATA | LIBXML_NONET);

$rows = [];
foreach ($xml->item as $it) {
    $rows[] = [
        'sortKey' => (string)$it->yourSortElement,
        'data'    => $it
    ];
}

usort($rows, function($a, $b){
    return strcmp($a['sortKey'], $b['sortKey']);
});

// now loop $rows to generate your HTML table

Troubleshooting and cautions

  • If file_get_contents() returns false, check allow_url_fopen, file permissions, or the upload flow (use $_FILES for uploads). asked to show the XML; that helps diagnose structure issues that mentioned.
  • For large files use XMLReader (streaming) to avoid memory spikes.
  • For untrusted XML disable network access during parsing (LIBXML_NONET) and avoid expanding external entities to prevent XXE.
  • Turn on PHP error reporting while debugging so parsing errors are visible.

If you post a small sample of the XML (structure and a couple of records) I can show the exact extraction and sorting code for that structure.

Recommended Answers

All 3 Replies

Member Avatar for Member #120589

Show the xml content

does $_GET["xmlFile"] contan the filename/url of the xml or the actual xml-content?

We need to see your target xml file. It all depends on your xml file structure. There are several methods in parsing xml file. It all depends on the target document.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.