1

I need to get <name> and <URL> tag's value where subtype="mytype".How can do it in PHP? I want document name and test.pdf path in my result.

<?xml version="1.0" encoding="UTF-8"?>
    <test>
        <required>
            <item type="binary">
                <name>The name</name>
            <url visibility="restricted">c:/temp/test/widget.exe</url>
            </item>
            <item type="document" subtype="mytype">
                <name>document name</name>
            <url visiblity="visible">c:/temp/test.pdf</url>
            </item>
        </required>
    </test>
3
  • Is the <a href="php.net/manual/en/book.xml.php">XML Parser</a> extension an option? Commented Oct 20, 2011 at 4:36
  • I'm not sure who marked you down - or why - but Phil is absolutely correct. SimpleXML (using XPath) is the way to go: w3schools.com/php/php_xml_simplexml.asp Commented Oct 20, 2011 at 4:39
  • @paulsm4: the downvote came from me. The reason was: no research effort shown, no code provided and the answer could be found by google or the SO search function. Commented Oct 20, 2011 at 5:04

3 Answers 3

3

Use SimpleXML and XPath, eg

$xml = simplexml_load_file('path/to/file.xml');

$items = $xml->xpath('//item[@subtype="mytype"]');
foreach ($items as $item) {
    $name = (string) $item->name;
    $url = (string) $item->url;
}
Sign up to request clarification or add additional context in comments.

2 Comments

@dayana I assumed your XML was in a string variable. I've updated my answer to use a file
@dayana Feel free to accept this answer. While you're at it, accept some answers for your other questions
2

PHP 5.1.2+ has an extension called SimpleXML enabled by default. It's very useful for parsing well-formed XML like your example above.

First, create a SimpleXMLElement instance, passing the XML to its constructor. SimpleXML will parse the XML for you. (This is where I feel the elegance of SimpleXML lies - SimpleXMLElement is the entire library's sole class.)

$xml = new SimpleXMLElement($yourXml);

Now, you can easily traverse the XML as if it were any PHP object. Attributes are accessible as array values. Since you're looking for tags with specific attribute values, we can write a simple loop to go through the XML:

<?php
$yourXml = <<<END
<?xml version="1.0" encoding="UTF-8"?>
    <test>
        <required>
            <item type="binary">
                <name>The name</name>
            <url visibility="restricted">c:/temp/test/widget.exe</url>
            </item>
            <item type="document" subtype="mytype">
                <name>document name</name>
            <url visiblity="visible">c:/temp/test.pdf</url>
            </item>
        </required>
    </test>
END;

// Create the SimpleXMLElement
$xml = new SimpleXMLElement($yourXml);

// Store an array of results, matching names to URLs.
$results = array();

// Loop through all of the tests
foreach ($xml->required[0]->item as $item) {
    if ( ! isset($item['subtype']) || $item['subtype'] != 'mytype') {
        // Skip this one.
        continue;
    }

    // Cast, because all of the stuff in the SimpleXMLElement is a SimpleXMLElement.
    $results[(string)$item->name] = (string)$item->url;
}

print_r($results);

Tested to be correct in codepad.

Hope this helps!

Comments

0

You can use the XML Parser or SimpleXML.

Comments

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.