0

Can someone please help me work out how to output the below XML file in PHP

    XML:
    <student>
    <name>studentname</name>
    <map>
    <competency>
      <level>5</level>
      <skill name="Maths"> 
         <skillinfo> "some value" </skillinfo>
      </skill>
    <competency>
  <competency>
      <level>4</level>
      <skill name="Science"> 
         <skillinfo> "some value" </skillinfo>
      </skill>
    <competency>
  <competency>
      <level>5</level>
      <skill name="Technology"> 
         <skillinfo> "some value" </skillinfo>
      </skill>
    <competency>
    </map>
    </student>

Code I've so far:

$dom = new DOMDocument;
$dom->loadXML($data);
$xpath = new DomXpath($dom);
$x = $dom->documentElement;

    echo "<table class='blueTable' >
  <thead>
    <tr>
      <th>Student Name</th>
      <th>Skill</th>
      <th>Level</th>
     </tr>
  </thead>
  <tbody>
   <tr> ";
    foreach ($xpath->query('//skill') as $item) {
        foreach($item->parentNode->childNodes as $node)
        if ($node->tagName == 'level')
             echo "<td>" . $node->nodeValue . "</td>";
    } 

The above code outputs all the levels found in the XML file for that student. Each student has their own XML file, but you don't need to worry about that complexity. Lets work out how to output just one student, their skill and level first.

I'm using XDOM and what I'd like to do is:

In a table I'd like list first the student name, skill name (maths), and the level information. This needs to be iterated as there are multiple skills and levels within each skill for each student.

I can work out how to find something via XPATH and then locate its parent but can't seem to figure out how to loop this properly. Please help :)

Thank you

5
  • 2
    Can you add what you have tried to your question? Commented Jun 4, 2018 at 7:18
  • where are the codes? and why do you need a loop? are there multiple student nodes in there? the xml above only shows 1 Commented Jun 4, 2018 at 7:21
  • @Ghost I assume he means loop over 'multiple skills and levels' although an example of this data would be useful. Commented Jun 4, 2018 at 7:23
  • @NigelRen or probably multiple competency nodes, we don't know yet, only the OP does Commented Jun 4, 2018 at 7:25
  • No there is only one student however each student has several Skill and each skill with varying skill level. I will update the question with an example. Commented Jun 4, 2018 at 7:35

1 Answer 1

1

This is an example using SimpleXMLElement to get your values. The elements are or type SimpleXMLElement and you could use (string) to get the value. You might use this to create your table.

$studentName = (string)$elm->name;
echo $studentName . "<br>";
foreach ($elm->map->competency as $comp) {
    $level = (string)$comp->level;
    $name = (string)$comp->skill->attributes()->name;
    $skillinfo = (string)$comp->skill->skillinfo;
    echo sprintf(
        "level: $level: %s<br> name: %s<br> skillinfo: %s",
        $level, $name, $skillinfo
    );
}

Demo

Sign up to request clarification or add additional context in comments.

3 Comments

no need for xpath, just use SimpleXML and access attributes and stuff + 1
@Ghost Of course, you are right. I have updated the code.
Thank you! (string) was the key I needed to know.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.