我在 php 中使用 xpath 从xml文件中提取数据,所以我的代码如下所示:
xml = simplexml_load_file("CCV.xml");
foreach ($xml->xpath('./section[@label="Education"]/section[@label="Degrees"]') as $degrees) {
var_dump($degrees);
}
在切换到上面这种代码之前,我使用的是XPath->查询(路径),它可以很好地返回我需要的数据。但现在我真的坚持这种阵列结构。我正在寻找具有相同问题的另一篇文章,但数组的结构更简单和全面。那么如何在这种情况下返回值博士:
object(SimpleXMLElement)[5]
public '@attributes' =>
array (size=3)
'id' => string 'aee5a225a504442fb83f716235cfb587' (length=32)
'label' => string 'Degrees' (length=7)
'recordId' => string '5b6f8e21156d44fa87cff8271b8bae84' (length=32)
public 'field' =>
array (size=13)
0 =>
object(SimpleXMLElement)[8]
public '@attributes' =>
array (size=2)
...
public 'lov' => string 'Doctorate' (length=9)
1 =>
object(SimpleXMLElement)[9]
public '@attributes' =>
array (size=2)
...
public 'value' => string 'Ph. D.' (length=6)
public 'bilingual' =>
object(SimpleXMLElement)[27]
...
2 =>
object(SimpleXMLElement)[10]
public '@attributes' =>
array (size=2)
...
public 'value' => string 'Software Engineering' (length=20)
public 'bilingual' =>
object(SimpleXMLElement)[28]
...
3 =>
object(SimpleXMLElement)[11]
public '@attributes' =>
array (size=2)
...
public 'value' => string 'Approximate Graph Matching for Software Engineering' (length=51)
4 =>
object(SimpleXMLElement)[12]
public '@attributes' =>
array (size=2)
...
public 'refTable' =>
object(SimpleXMLElement)[29]
...
5 =>
object(SimpleXMLElement)[13]
public '@attributes' =>
array (size=2)
...
public 'value' =>
object(SimpleXMLElement)[34]
...
6 =>
object(SimpleXMLElement)[14]
public '@attributes' =>
array (size=2)
...
7 =>
object(SimpleXMLElement)[15]
public '@attributes' =>
array (size=2)
...
8 =>
object(SimpleXMLElement)[16]
public '@attributes' =>
array (size=2)
...
public 'lov' => string 'Completed' (length=9)
9 =>
object(SimpleXMLElement)[17]
public '@attributes' =>
array (size=2)
...
public 'value' => string '2007/9' (length=6)
10 =>
object(SimpleXMLElement)[18]
public '@attributes' =>
array (size=2)
...
public 'value' => string '2011/8' (length=6)
11 =>
object(SimpleXMLElement)[19]
public '@attributes' =>
array (size=2)
...
public 'value' =>
object(SimpleXMLElement)[35]
...
12 =>
object(SimpleXMLElement)[20]
public '@attributes' =>
array (size=2)
...
public 'section' =>
array (size=6)
0 =>
object(SimpleXMLElement)[21]
public '@attributes' =>
array (size=3)
...
public 'field' =>
array (size=2)
...
1 =>
object(SimpleXMLElement)[22]
public '@attributes' =>
array (size=3)
...
public 'field' =>
array (size=2)
...
2 =>
object(SimpleXMLElement)[23]
public '@attributes' =>
array (size=3)
...
public 'field' =>
array (size=2)
...
3 =>
object(SimpleXMLElement)[24]
public '@attributes' =>
array (size=3)
...
public 'field' =>
array (size=3)
...
4 =>
object(SimpleXMLElement)[25]
public '@attributes' =>
array (size=3)
...
public 'field' =>
array (size=3)
...
5 =>
object(SimpleXMLElement)[26]
public '@attributes' =>
array (size=3)
...
public 'field' =>
array (size=2)
...
Xml文件:
<section id="aee5a225a504442fb83f716235cfb587" label="Degrees" recordId="5b6f8e21156d44fa87cff8271b8bae84">
<field id="a83a0af883924c57bb66107cc32b6d5e" label="Degree Type">
<lov id="00000000000000000000000000000073">Doctorate</lov>
</field>
<field id="7df537009941493789a32bcae3499909" label="Degree Name">
<value type="Bilingual">Ph. D.</value>
<bilingual>
<english>Ph. D.</english>
</bilingual>
</field>
<field id="35696972e69541dd86a80521d3737b26" label="Specialization">
<value type="Bilingual">Software Engineering</value>
<bilingual>
<english>Software Engineering</english>
</bilingual>
</field>
<field id="ca55badff5c1483cbb1c69495a22d0af" label="Thesis Title">
<value type="String">Approximate Graph Matching for Software Engineering</value>
</field>
<field id="6e225f28f003427aa79aa68ff0aa3865" label="Organization">
<refTable label="Organization" refValueId="00000000000000000000011930246375">
<linkedWith label="Country" refOrLovId="00000000000000000000000000002000" value="Canada"/>
<linkedWith label="Subdivision" refOrLovId="00000000000000000000000000100000" value="Quebec"/>
<linkedWith label="Organization Type" refOrLovId="00000000000000000000000000030000" value="Academic"/>
<linkedWith label="Organization" refOrLovId="ee597e9073b6479b94f903ca08f81903" value="École Polytechnique de Montréal"/>
</refTable>
</field>
<field id="020ec1f40f3d4065bf5424f77209b8e4" label="Other Organization">
<value type="String"/>
</field>
<field id="f1a127d96eb64b1e8776023106ac4a00" label="Other Organization Type"/>
<field id="fc5c85c9557441399dba19033697d56d" label="Other Organization Location"/>
<field id="c42a1deb6fe046dfa5502169032760de" label="Degree Status">
<lov id="00000000000000000000000000000068">Completed</lov>
</field>
<field id="337ee6b2606c4c899f0e0c4ec3bd6ec2" label="Degree Start Date">
<value format="yyyy/MM" type="YearMonth">2007/9</value>
</field>
<field id="4b818aef68a84743b19149d376032afb" label="Degree Received Date">
<value format="yyyy/MM" type="YearMonth">2011/8</value>
</field>
<field id="91b70ad64b6843fdaae1e94282bc77f0" label="Degree Expected Date">
<value format="yyyy/MM" type="YearMonth"/>
</field>
<field id="b04a491dc6ea4fc99e2453dbfb834965" label="Transferred to PhD without completing Masters?"/>
<section id="447d27e319c94c2dbb661959c7c40c97" label="Areas of Research" recordId="4025fa6db2ce411cacbdc64b653ede39">
<field id="a8b0b7584b454b3c96fc34882a7bae12" label="Order">
<value type="Number">1</value>
</field>
<field id="e7677e7cec974b3d81196938648b731f" label="Area of Research">
<refTable label="Area of Research" refValueId="00000000000000000000063990580350">
<linkedWith label="Sector of Research" refOrLovId="00000000000000000000000100003800" value="Natural Sciences and Engineering"/>
<linkedWith label="Field" refOrLovId="28b7043d4ec74915a90ee44bbf5ec71f" value="Abstract Structures"/>
<linkedWith label="Subfield" refOrLovId="1bb709ee05854387a897b8a17cfd3f0e" value="Computer Sciences and Mathematical Tools"/>
<linkedWith label="Area" refOrLovId="75f906df893642f895b9203370e0b9a9" value="Software (Tools)"/>
</refTable>
</field>
</section>
<section id="447d27e319c94c2dbb661959c7c40c97" label="Areas of Research" recordId="daab01e02aae4a00b6802f32bb3b2b26">
<field id="a8b0b7584b454b3c96fc34882a7bae12" label="Order">
<value type="Number">2</value>
</field>
<field id="e7677e7cec974b3d81196938648b731f" label="Area of Research">
<refTable label="Area of Research" refValueId="00000000000000000000080370827290">
<linkedWith label="Sector of Research" refOrLovId="00000000000000000000000100003800" value="Natural Sciences and Engineering"/>
<linkedWith label="Field" refOrLovId="28b7043d4ec74915a90ee44bbf5ec71f" value="Abstract Structures"/>
<linkedWith label="Subfield" refOrLovId="1bb709ee05854387a897b8a17cfd3f0e" value="Computer Sciences and Mathematical Tools"/>
<linkedWith label="Area" refOrLovId="75f906df893642f895b9203370e0b9a9" value="Optimization, Control and Operations Research"/>
</refTable>
</field>
</section>
<section id="42e0ec8b7946411f9b2a0f2093ce2f1e" label="Research Disciplines" recordId="abc11eb9dd034caeabada014f79c48d6">
<field id="7f0f0c79f847413cbdefbd52dee958dd" label="Order">
<value type="Number">1</value>
</field>
<field id="c437657271cf457fa2b516dbd1d57efc" label="Research Discipline">
<refTable label="Research Discipline" refValueId="00000000000000000000082120706250">
<linkedWith label="Sector of Discipline" refOrLovId="00000000000000000000000100003700" value="Natural Sciences and Engineering"/>
<linkedWith label="Field" refOrLovId="1add935c1fbc44ec96e9edfe94ad6863" value="Engineering"/>
<linkedWith label="Discipline" refOrLovId="4ac3aff1a8314560b7b7e048099e7996" value="Computer Engineering and Software Engineering"/>
</refTable>
</field>
</section>
<section id="49eb8b6f4d0c4e3aa68dbe242bdecfb4" label="Supervisors" recordId="6e6c66c147b5411e975f7264cb43999e">
<field id="c5d78ee68528412d892acc849851dbe6" label="Supervisor Name">
<value type="String">Philippe Galinier</value>
</field>
<field id="93f1886a6ace43659d519b628e9f026e" label="Start Date">
<value format="yyyy/MM" type="YearMonth">2007/9</value>
</field>
<field id="7dd6df1257ef461f806b5e644741e1a2" label="End Date">
<value format="yyyy/MM" type="YearMonth">2011/8</value>
</field>
</section>
<section id="49eb8b6f4d0c4e3aa68dbe242bdecfb4" label="Supervisors" recordId="c4bd2c87a6154afd93b0254147975ece">
<field id="c5d78ee68528412d892acc849851dbe6" label="Supervisor Name">
<value type="String">Giulio Antoniol</value>
</field>
<field id="93f1886a6ace43659d519b628e9f026e" label="Start Date">
<value format="yyyy/MM" type="YearMonth">2007/9</value>
</field>
<field id="7dd6df1257ef461f806b5e644741e1a2" label="End Date">
<value format="yyyy/MM" type="YearMonth">2011/8</value>
</field>
</section>
<section id="14f15c08598c4baa911697a16157d75a" label="Fields of Application" recordId="a5c79fac596441e9ba0a19bd25148e42">
<field id="4e269294415249079731231561dfec8b" label="Order">
<value type="Number">1</value>
</field>
<field id="99e0bcb3d8344827915f282ec3ca7e3a" label="Field of Application">
<refTable label="Field of Application" refValueId="00000000000000000000000908181810">
<linkedWith label="Field of Application" refOrLovId="00000000000000000000000100003900" value="Science and Technologies"/>
<linkedWith label="Subfield" refOrLovId="bb37c5edd6464aa3a99abcaaf1251bed" value="Communication and Information Technologies"/>
</refTable>
</field>
</section>
</section>
答案 0 :(得分:0)
正如评论中所接受的那样
foreach ($xml->field as $f) {
$a=$f->attributes();
if ('Degree Type'==$a['label']){
$x=$f->lov;break;
}
}
答案 1 :(得分:0)
上面的代码非常适用于 simplexml_load_string()。但在我的情况下,它不起作用,我得到x变量undefined。
$xml = simplexml_load_file("CCV.xml");
// foreach ($xml->xpath('./section[@label="Education"]/section[@label="Degrees"]') as $details) {
foreach ($xml->field as $details) {
$a = $details->attributes();
if ('Degree Type'== $a['label']){
$x=$details->lov;
break;
}
}
var_export($x);
我有3度。所以我想向他们展示(博士,硕士和学士)