根据特定的属性解析PHP中的XML

时间:2021-02-20 15:30:45

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.

我需要获取 标签的值,其中subtype="mytype"。PHP怎么做呢?我需要文档名称和测试。pdf路径在我的结果中。

<?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 个解决方案

#1


3  

Use SimpleXML and XPath, eg

使用SimpleXML和XPath

$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;
}

#2


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.

PHP 5.1.2+默认启用了一个名为SimpleXML的扩展。它对于解析格式良好的XML非常有用,如上面的示例。

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.)

首先,创建一个SimpleXMLElement实例,将XML传递给它的构造函数。SimpleXML将为您解析XML。(这就是SimpleXML的优雅之处——SimpleXMLElement是整个库的惟一类。)

$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:

现在,您可以轻松地遍历XML,就好像它是任何PHP对象一样。属性可作为数组值访问。因为您正在寻找具有特定属性值的标记,所以我们可以编写一个简单的循环来遍历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!

希望这可以帮助!

#3


0  

You can use the XML Parser or SimpleXML.

您可以使用XML解析器或SimpleXML。

#1


3  

Use SimpleXML and XPath, eg

使用SimpleXML和XPath

$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;
}

#2


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.

PHP 5.1.2+默认启用了一个名为SimpleXML的扩展。它对于解析格式良好的XML非常有用,如上面的示例。

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.)

首先,创建一个SimpleXMLElement实例,将XML传递给它的构造函数。SimpleXML将为您解析XML。(这就是SimpleXML的优雅之处——SimpleXMLElement是整个库的惟一类。)

$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:

现在,您可以轻松地遍历XML,就好像它是任何PHP对象一样。属性可作为数组值访问。因为您正在寻找具有特定属性值的标记,所以我们可以编写一个简单的循环来遍历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!

希望这可以帮助!

#3


0  

You can use the XML Parser or SimpleXML.

您可以使用XML解析器或SimpleXML。