0

Using the code here: http://www.w3schools.com/php/php_xml_parser_expat.asp

How would you get the attributes using the switch (if possible) for each one if the XML file was like this:

<?xml version="1.0" encoding="ISO-8859-1"?>
<note>
<to id="01">Tove</to>
<from id="02">Jani</from>
<heading color="#CEAF33">Reminder</heading>
<body type="small" important="low">Don't forget me this weekend!</body>
</note> 

2 Answers 2

1

I wouldn't use W3's tutorial. I'd use simplexml_load_string to load your string into an XML object, then iterate over it like so:

$notes = simplexml_load_string( $xml);
foreach( $notes as $note)
{
    echo $note . "[" . $note->getName() . "]\n";
    foreach( $note->attributes() as $key => $value)
    {
        echo "\t" . $key . '=' . $value . "\n";
    }
    echo "\n";
}

Example (uses your input string)

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

5 Comments

How can I do this with an API that doesn't have a doctype? I want to use W3Schools tutorial, because that's what I've learned from. I have everything almost ready and all I need to do is get the attributes from the API.
SimpleXML doesn't require an HTML doctype definition, it just needs to be valid XML (i.e. start with a <?xml ...?> tag and contain a top level XML node). W3Schools tutorial is cumbersome and inefficient, you should be able to use this in any setup.
The api I use doesn't have an <?xml ... ?> beginning. How do I validate it? I tried using simplexml_load_file(file.xml,LIBXML_DTDVALID(1)); but it doesn't work. Do I really need to create a XML document using code just to validate it?
If it's outputting XML, just concatenate a simple <?xml ?> tag to the beginning so simplexml_load_string works.
thanks for the example :) I'm going to work it with APIs. It does look a lot simpler. Now I just have to figure out how I can do different things with each element.
1

You should use the SimpleXML extension, it is much simpler and easier to work with.

From the PHP docs:

<?php
$string = <<<XML
<a>
 <foo name="one" game="lonely">1</foo>
</a>
XML;

$xml = simplexml_load_string($string);
foreach($xml->foo[0]->attributes() as $a => $b) {
    echo $a,'="',$b,"\"\n";
}
?>

See https://www.php.net/manual/en/book.simplexml.php for more information.

3 Comments

How can I do this with an API that doesn't have a doctype? I want to use W3Schools tutorial, because that's what I've learned from. I have everything almost ready and all I need to do is get the attributes from the API.
Here is a guide on dealing with common XML errors. au2.php.net/manual/en/simplexml.examples-errors.php
Also, I understand that you want to use the W3Schools tutorial, but seriously, it is completely outdated and takes a lot longer to implement. SimpleXML was designed to improve the horrible XML situation we had in PHP.

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.