Get value of attribut - just getting the first letter of each one ?!!
Submitted by lwebb on Wed, 2009-04-15 23:20.Magic Parser
Hi,
I'm using the following script to sort my datas but something very special happens with me, I'm just getting the first charachter of each attribut.
Here'is the XML file for example:
<?xml version="1.0" encoding="UTF-8"?>
<response>
<result name="GetTestList">
<records>
<record test_id="6682" test_name="Demo Editing Skills Certification" coverage="Prepositions ;Parallel Construction ;Comma Usage ;Punctuation ;Misplaced Modifiers ;Apostrophe Usage ;Sentence Structure ;Pronoun Usage ;Tenses ;Article Usage" total_questions="10" duration="10" passing_marks="50" category="Demo Tests"/>
<record test_id="6683" test_name="Demo Adobe Photoshop CS3 Test" coverage="Layers ;Type ;Automating Tasks and Keyboard Shortcuts ;Workspace ;Working with Images, Retouching and Transforming ;Color Management ;Color and Tonal Adjustments ;Using Selection and Drawing ;Filters and Saving Images ;Working With Web, Video and Animation" total_questions="10" duration="10" passing_marks="50" category="Demo Tests"/>
<record test_id="6684" test_name="Demo Programming with C++ Test" coverage="Classes ;Constructors ;Variables and Datatypes" total_questions="10" duration="10" passing_marks="50" category="Demo Tests"/>
<record test_id="6685" test_name="Demo HTML 4.01 Test" coverage="Advanced Tags ;Fundamentals ;Tags ;Tables ;Links and Images ;Forms and Frames" total_questions="10" duration="10" passing_marks="50" category="Demo Tests"/>
</records>
</result>
</response>And the PHP script with the Magic Parser:
<?php
require("MagicParser.php");
$filename = "note.xml";
$format_string = MagicParser_getFormat($filename);
if (!$format_string)
{ print "<p>".MagicParser_getErrorMessage()."</p>";
exit;
}
else
{ print "<p><strong>Autodetected Format String:</strong> ".$format_string."</p>"; }
print "<p>Contents of first record:</p>";
function myRecordHandler($record)
{ print "<table border='1'>";
foreach($record as $key => $value)
{
print "<tr>";
print "<th>".$key."</th>";
print "<td>".htmlentities($value)." </td>";
print "</tr>";
print $value['record-test_id']; // I'm trying to get the value of the attribut 'test-id' in node 'record'
}
print "</table>";
// return a non-zero value to stop reading any more records
return 0;
}
MagicParser_parse($filename,"myRecordHandler",$format);
?>At the end, I need the value of every attribut in a variable to store it in a MySQL Database.
If you also can help me with this, it will be very appreciated!
Any idea?
Thanks.
Hi!
When using the foreach loop to display every item in $record; if you still want to access individual elements you just need to refer to the $record array directly, rather than $value which is not actually an array...
In other words, instead of:
foreach($record as $key => $value){
print "<tr>";
print "<th>".$key."</th>";
print "<td>".htmlentities($value)." </td>";
print "</tr>";
print $value['record-test_id']; // I'm trying to get the value of the attribut 'test-id' in node 'record'
}
print "</table>";
...use:
foreach($record as $key => $value){
print "<tr>";
print "<th>".$key."</th>";
print "<td>".htmlentities($value)." </td>";
print "</tr>";
}
print "</table>";
print $record['record-test_id'];
Cheers,
David.