使用 PHP 解析 XML

发布于 10-18 10:00 字数 2419 浏览 6 评论 0原文

我正在尝试使用 PHP 的 SimpleXML 解析职位提要。我以前只使用过 JSON,但在让解析器工作时遇到问题。这是一些示例数据:

<shrs>
    <rq url="http://api.simplyhired.com/a/jobs-api/xml_v2/q-comission">
        <t>Comission Jobs</t>
        <dt>2011-02-18T23:58:38Z</dt>
        <si>0</si>
        <rpd>10</rpd>
        <tr>192</tr>
        <tv>146</tv>
        <em url=""/>
        <h>
            <kw pos="1"/>
        </h>
    </rq>
    <rs>
        <r>
            <jt>Virtual Recruiter (IT) - Comission ...</jt>
            <cn url="">Remedy Intelligent Staffing</cn>
            <src url="http://api.simplyhired.com/a/job-details/view/jobkey-monster91949932/cjp-0/hits-192?aff_id=28700">Monster</src>
            <ty>organic</ty>
            <loc cty="Buffalo" st="NY" postal="14211" county="" region="" country="US">Buffalo, NY</loc>
            <ls>2011-02-04T05:51:17Z</ls>
            <dp>2011-02-04T05:51:17Z</dp>
            <e>
    Seeking a candidate with previous recruiting experience to work as a Virtual Recruiter for a large client in the IT industry.a Responsibilities: Will recruit, screen, interview, and place candidates for many openings throughout the US Will...
    </e>
        </r>
        <r>
            <jt>Virtual Loan Officer (Mortgage) draw vs comission</jt>
            <cn url="">Netbranchology.com</cn>
            <src url="http://api.simplyhired.com/a/job-details/view/jobkey-7114.353281/cjp-2/hits-192?aff_id=28700">netbranchology.com</src>
            <ty>organic</ty>
            <loc cty="Denver" st="CO" postal="80218" county="" region="" country="US">Denver, CO</loc>
            <ls>2011-02-10T11:47:50Z</ls>
            <dp>2011-01-26T11:36:18Z</dp>
            <e>
    Minimize your overhead by becoming a virtual loan officer... Our client, a Texas-based mortgage banker, has just launched an innovative new program that lets you work from anywhere to originate residential mortgage loans. No office is...
    </e>
        </r>
    </rs>
</shrs>

[等]

我想将标签中的元数据检索到变量中,然后循环遍历每个作业结果来处理它。我怎样才能用 PHP 做到这一点? (到目前为止我一直在使用 SimpleXML 函数)

I'm trying to parse a jobs feed using PHP's SimpleXML. I've only used JSON before and am having problems getting the parser to work. Here's some sample data:

<shrs>
    <rq url="http://api.simplyhired.com/a/jobs-api/xml_v2/q-comission">
        <t>Comission Jobs</t>
        <dt>2011-02-18T23:58:38Z</dt>
        <si>0</si>
        <rpd>10</rpd>
        <tr>192</tr>
        <tv>146</tv>
        <em url=""/>
        <h>
            <kw pos="1"/>
        </h>
    </rq>
    <rs>
        <r>
            <jt>Virtual Recruiter (IT) - Comission ...</jt>
            <cn url="">Remedy Intelligent Staffing</cn>
            <src url="http://api.simplyhired.com/a/job-details/view/jobkey-monster91949932/cjp-0/hits-192?aff_id=28700">Monster</src>
            <ty>organic</ty>
            <loc cty="Buffalo" st="NY" postal="14211" county="" region="" country="US">Buffalo, NY</loc>
            <ls>2011-02-04T05:51:17Z</ls>
            <dp>2011-02-04T05:51:17Z</dp>
            <e>
    Seeking a candidate with previous recruiting experience to work as a Virtual Recruiter for a large client in the IT industry.a Responsibilities: Will recruit, screen, interview, and place candidates for many openings throughout the US Will...
    </e>
        </r>
        <r>
            <jt>Virtual Loan Officer (Mortgage) draw vs comission</jt>
            <cn url="">Netbranchology.com</cn>
            <src url="http://api.simplyhired.com/a/job-details/view/jobkey-7114.353281/cjp-2/hits-192?aff_id=28700">netbranchology.com</src>
            <ty>organic</ty>
            <loc cty="Denver" st="CO" postal="80218" county="" region="" country="US">Denver, CO</loc>
            <ls>2011-02-10T11:47:50Z</ls>
            <dp>2011-01-26T11:36:18Z</dp>
            <e>
    Minimize your overhead by becoming a virtual loan officer... Our client, a Texas-based mortgage banker, has just launched an innovative new program that lets you work from anywhere to originate residential mortgage loans. No office is...
    </e>
        </r>
    </rs>
</shrs>

[etc]

I'd like to retrieve the metadata in the tags into variables, and then loop through each job result under to process it. How can I do this with PHP? (I've been playing around with the SimpleXML functions so far)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

我三岁2024-10-25 10:00:07

节点作为对象属性进行访问,属性使用数组表示法。 foreach 允许您迭代节点。您可以通过将节点转换为字符串来获取节点的内容。 (所以如果你使用 echo 它是隐含的)

$shrs = simplexml_load_string($xml);

foreach ($shrs->rs->r as $r)
{
    $jobTitle = $r->jt;
    $city = $r->loc['cty'];

    echo "There's an offer for $jobTitle in $city<br />\n";
}

Nodes are accessed as object properties, attributes use the array notation. foreach lets you iterate over nodes. You can get the content of a node by casting it as a string. (so if you use echo it's implied)

$shrs = simplexml_load_string($xml);

foreach ($shrs->rs->r as $r)
{
    $jobTitle = $r->jt;
    $city = $r->loc['cty'];

    echo "There's an offer for $jobTitle in $city<br />\n";
}
魂ガ小子2024-10-25 10:00:07

尝试 SimpleXML:http://www.php.net/manual/en/book。 simplexml.php

它将把你的 XML 解析成一个漂亮的对象。

编辑:以下是如何使用它(假设您的 xml 存储在变量 $xml 中):

$xmlObject = new SimpleXMLElement($xml);

// to retrieve "http://api.simplyhired.com/a/jobs-api/xml_v2/q-comission"
$url = $xmlObject->rq['url'];

// to retrieve "Comission Jobs"
$t = $xmlObject->rq->t;
...

希望它有帮助。

Try SimpleXML: http://www.php.net/manual/en/book.simplexml.php

It will parse your XML into a nice object.

Edit: here's how to use it (assumes your xml is stored in the variable $xml):

$xmlObject = new SimpleXMLElement($xml);

// to retrieve "http://api.simplyhired.com/a/jobs-api/xml_v2/q-comission"
$url = $xmlObject->rq['url'];

// to retrieve "Comission Jobs"
$t = $xmlObject->rq->t;
...

Hope it helps.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文