简体   繁体   English

用PHP解析XML标签

[英]Parsing XML tags with PHP

I have an XML document that looks something like: 我有一个看起来像这样的XML文档:

<?xml version='1.0' encoding='UTF-8' standalone='yes' ?>
<smses count="1992">
  <sms protocol="0" address="5558675309" date="1309444177931" type="1" subject="null" body="text message" toa="0" sc_toa="0" service_center="null" read="1" status="-1" locked="0" />
</smses>

I want to extract the address, date, and body for each <sms> line, and there's about 8000 lines. 我想提取每个<sms>行的地址,日期和正文,大约有8000行。 I'm not sure the best way to go about this, so if anyone could point me in the right direction, I'd appreciate it. 我不确定执行此操作的最佳方法,因此如果有人可以指出正确的方向,我将不胜感激。 Don't really need specific code, just direction. 确实不需要特定的代码,只需指示即可。 I'm stumped. 我很沮丧

$dom = new DOMDOcument();

// Load your XML as a string
$dom->loadXML($s);

// Create new XPath object
$xpath = new DOMXpath($dom);

// Query for Account elments inside NewDataSet elemts inside string elements
$result = $xpath->query("/smses");

// Note there are many ways to query XPath using this syntax

// Iterate over the results
foreach($result as $node)
{
    // Obtains item for sms tags here
}

You can use PHP's SimpleXML extension to parse this. 您可以使用PHP的SimpleXML扩展来对此进行解析。 See "Basic SimpleXML usage" for an introduction. 有关简介,请参见“基本SimpleXML用法”

Here's some code to get you started ( array_map requires PHP >= 5.3): 这是一些入门的代码( array_map需要PHP> = 5.3):

$smses = new SimpleXMLElement($xml_str);
$smses_parsed = array_map(function($sms_el) {
        return array('address' => (string)$sms_el['address'],
            'date' => (int)$sms_el['date'],
            'body' => (string)$sms_el['body']);
    }, $smses);

print_r($smses_parsed[0]); /* => array("address" => "5558675309",
                                       "date" => 1309444177931,
                                       "body" => "text message") */

One note: SimpleXML is a strict parser. 注意事项:SimpleXML是严格的解析器。 If your XML is somewhat malformed, you'll probably have more luck with DOMDocument . 如果您的XML格式有误,那么DOMDocument可能会给您带来更多的运气。 (I don't expect that case to be likely here, though, given the simple document structure you posted.) (不过,鉴于您发布的文档结构简单,我不希望这种情况在这里出现。)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM