gpt4 book ai didi

php - 用 PHP 解析 XML

转载 作者:数据小太阳 更新时间:2023-10-29 02:32:20 26 4
gpt4 key购买 nike

我正在尝试使用 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 函数)

最佳答案

节点作为对象属性访问,属性使用数组表示法。 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";
}

关于php - 用 PHP 解析 XML,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5048858/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com