如何使用PHP / HTML从xml提取数据

时间:2019-03-04 21:55:19

标签: php html xml

我正在研究一个学习单位的项目,因此我需要从xml中提取所有数据,这是一个用reload ld编辑器制作的xml的示例

<?xml version="1.0" encoding="UTF-8"?>
<!--This is a Reload LD Editor version 2.1.3 IMS Learning Design 1.0 document-->
<!--Spawned from the Reload Learning Design Generator - http://www.reload.ac.uk-->
<manifest xmlns="http://www.imsglobal.org/xsd/imscp_v1p1" xmlns:imsld="http://www.imsglobal.org/xsd/imsld_v1p0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.imsglobal.org/xsd/imscp_v1p1 http://www.imsglobal.org/xsd/imscp_v1p1.xsd http://www.imsglobal.org/xsd/imsld_v1p0 http://www.imsglobal.org/xsd/IMS_LD_Level_A.xsd" identifier="manifest-7EE42F26-6089-C6C2-6847-BE6E7B7CCB53">
  <organizations>
<imsld:learning-design identifier="ld-56153381-4457-f4dd-ddd8-04b623b7b3ca" level="A" sequence-used="false" uri="http://www.reload.ac.uk/uri/ld-51460f5e-71bd-3950-fad2-7522c7018c2f" version="1">
  <imsld:title>prueba</imsld:title>
  <imsld:learning-objectives>
    <imsld:title>hacer pruebas</imsld:title>
    <imsld:item identifier="item-c9b554d7-9388-3f18-93a0-3a6a599d6d67" identifierref="resource-58164a05-d1a7-73a5-2fe9-5b3a0a9522be" isvisible="true">
      <imsld:title>pruebaoverview</imsld:title>
    </imsld:item>
  </imsld:learning-objectives>
  <imsld:components>
    <imsld:roles>
      <imsld:learner identifier="role-79d92c95-7ec4-0cc0-711a-963a476e2a63">
        <imsld:title>Alumno3</imsld:title>
        <imsld:information>
          <imsld:title>Alumno</imsld:title>
          <imsld:item identifier="item-6e199a21-f143-e27a-adbc-2ab2d2de391c" identifierref="resource-c7f25198-8dce-d046-4594-7a2b22b5dd15" isvisible="true" />
        </imsld:information>
      </imsld:learner>
      <imsld:staff identifier="role-0964b724-355c-49e8-a60f-58b444b29af4">
        <imsld:title>Asesor</imsld:title>
        <imsld:information>
          <imsld:title>Profesor1</imsld:title>
          <imsld:item identifier="item-7fe2de65-6412-0bb8-fcbe-caeb0e6abe20" identifierref="resource-f8de38c1-d7f8-a9a5-b7a4-c737d967daf1" isvisible="true">
            <imsld:title>profesor1</imsld:title>
          </imsld:item>
        </imsld:information>
      </imsld:staff>
      <imsld:staff identifier="role-859e8d69-def4-6421-d721-52f0dbd26897">
        <imsld:title>Tutor</imsld:title>
        <imsld:information>
          <imsld:title>Profesor2</imsld:title>
          <imsld:item identifier="item-d76d9e97-97d3-47bd-0dcb-d2a224a77cd4" identifierref="resource-37f8f529-49c1-552c-e0a4-8aa581d93aa5" isvisible="true">
            <imsld:title>profesor2</imsld:title>
          </imsld:item>
        </imsld:information>
      </imsld:staff>
    </imsld:roles>
    <imsld:activities>
      <imsld:learning-activity identifier="la-ce439085-5259-457c-bb05-0fbcb5f983a8" isvisible="true">
        <imsld:title>LA1</imsld:title>
        <imsld:activity-description>
          <imsld:title>LA1</imsld:title>
          <imsld:item identifier="item-6766cc5d-a193-ba4a-6af3-5eccbd9e77ee" identifierref="resource-f6455607-038b-f81a-b59e-98c9e395eeb9" isvisible="true">
            <imsld:title>la1</imsld:title>
          </imsld:item>
        </imsld:activity-description>
      </imsld:learning-activity>
      <imsld:learning-activity identifier="la-fb309770-9e97-7986-213f-f866046208ac" isvisible="true">
        <imsld:title>LA2</imsld:title>
        <imsld:activity-description>
          <imsld:title>LA2</imsld:title>
          <imsld:item identifier="item-afdf282b-959b-bfc3-cf96-de476fc9c41c" isvisible="true">
            <imsld:title>la2</imsld:title>
          </imsld:item>
        </imsld:activity-description>
      </imsld:learning-activity>
      <imsld:learning-activity identifier="la-ece94eac-6168-0ffd-35fd-ebc4dc233af1" isvisible="true">
        <imsld:title>LA3</imsld:title>
        <imsld:activity-description>
          <imsld:title>LA3</imsld:title>
          <imsld:item identifier="item-1f48b95d-412a-2c4e-8620-aca7e0a3109b" identifierref="resource-d54d1b13-cb38-f478-a40e-bbe0d950357a" isvisible="true">
            <imsld:title>la3</imsld:title>
          </imsld:item>
        </imsld:activity-description>
      </imsld:learning-activity>
      <imsld:support-activity identifier="sa-fd2d3078-3468-f7a5-324c-cc438e1f2c8c" isvisible="true">
        <imsld:title>SA1</imsld:title>
        <imsld:role-ref ref="role-0964b724-355c-49e8-a60f-58b444b29af4" />
        <imsld:activity-description>
          <imsld:title>SA1</imsld:title>
          <imsld:item identifier="item-f757db16-681a-8598-d4cd-a271c35ba503" identifierref="resource-961c7c0a-8ca4-8c20-aea2-65439c02941e" isvisible="true" />
        </imsld:activity-description>
      </imsld:support-activity>
      <imsld:support-activity identifier="sa-5566b272-8784-92c8-5429-9493f734278b" isvisible="true">
        <imsld:title>SA2</imsld:title>
        <imsld:role-ref ref="role-0964b724-355c-49e8-a60f-58b444b29af4" />
        <imsld:activity-description>
          <imsld:title>SA2</imsld:title>
          <imsld:item identifier="item-6d2db837-6f2a-2d6b-211e-7fd04ebd94f5" identifierref="resource-f15c67c4-7a03-8ff7-0bd2-541a1859e985" isvisible="true" />
        </imsld:activity-description>
      </imsld:support-activity>
      <imsld:support-activity identifier="sa-5ebeaf81-2f76-a6f2-51c5-8f4aa1c62d0a" isvisible="true">
        <imsld:title>SA3</imsld:title>
        <imsld:role-ref ref="role-79d92c95-7ec4-0cc0-711a-963a476e2a63" />
        <imsld:activity-description>
          <imsld:title>SA3</imsld:title>
          <imsld:item identifier="item-1a07bf78-8429-bd00-7f47-ca8904e49f99" identifierref="resource-8e804d72-e9aa-51ff-3e4a-c00a2e731aaa" isvisible="true" />
        </imsld:activity-description>
      </imsld:support-activity>
      <imsld:support-activity identifier="sa-af662654-3139-7f1d-84d1-8726d7aeb5f6" isvisible="true">
        <imsld:title>SA4</imsld:title>
        <imsld:role-ref ref="role-79d92c95-7ec4-0cc0-711a-963a476e2a63" />
        <imsld:activity-description>
          <imsld:title>SA4</imsld:title>
          <imsld:item identifier="item-4a77d62c-de4a-695b-2ce4-d06520ceb327" identifierref="resource-51088ea6-51d4-7e18-f62b-76cbcb650329" isvisible="true">
            <imsld:title>sa4</imsld:title>
          </imsld:item>
        </imsld:activity-description>
      </imsld:support-activity>
    </imsld:activities>
  </imsld:components>
  <imsld:method>
    <imsld:play identifier="play-01972078-c1a1-8467-dfb0-fe8bb14d7a71" isvisible="true">
      <imsld:title>NP1</imsld:title>
      <imsld:act identifier="act-95446b8b-ee43-67ad-83ca-554220bf90d7">
        <imsld:title>Act</imsld:title>
        <imsld:role-part identifier="rolepart-769ab839-497e-fdca-efa0-8c0a6cd73a65">
          <imsld:title>Role Part</imsld:title>
          <imsld:role-ref ref="role-0964b724-355c-49e8-a60f-58b444b29af4" />
          <imsld:support-activity-ref ref="sa-fd2d3078-3468-f7a5-324c-cc438e1f2c8c" />
        </imsld:role-part>
      </imsld:act>
    </imsld:play>
    <imsld:play identifier="play-5f25c22e-0387-6144-9ab4-d109aa5abb1b" isvisible="true">
      <imsld:title>NP2</imsld:title>
      <imsld:act identifier="act-55373ff5-835f-d1ba-fc3e-d069c5b27547">
        <imsld:title>Act</imsld:title>
        <imsld:role-part identifier="rolepart-0269f77e-b009-3256-f12a-9742fb4462e0">
          <imsld:title>Role Part</imsld:title>
          <imsld:role-ref ref="role-79d92c95-7ec4-0cc0-711a-963a476e2a63" />
          <imsld:support-activity-ref ref="sa-5ebeaf81-2f76-a6f2-51c5-8f4aa1c62d0a" />
        </imsld:role-part>
      </imsld:act>
    </imsld:play>
    <imsld:play identifier="play-3d7ac7cb-c36b-bc8e-a841-bfc67c6f5c02" isvisible="true">
      <imsld:title>NP3</imsld:title>
      <imsld:act identifier="act-31393426-8531-1605-2ed3-d8798cd4b149">
        <imsld:title>Act</imsld:title>
        <imsld:role-part identifier="rolepart-ce3e3669-90d7-8198-68d9-8bc0524f3192">
          <imsld:title>Role Part</imsld:title>
          <imsld:role-ref ref="role-0964b724-355c-49e8-a60f-58b444b29af4" />
          <imsld:support-activity-ref ref="sa-5566b272-8784-92c8-5429-9493f734278b" />
        </imsld:role-part>
      </imsld:act>
    </imsld:play>
    <imsld:play identifier="play-0d3315a1-2f25-ab0a-a461-2094fbfae9a4" isvisible="true">
      <imsld:title>NP4</imsld:title>
      <imsld:act identifier="act-4a08f369-4dea-474c-8a27-6c5d39304a7c">
        <imsld:title>Act</imsld:title>
        <imsld:role-part identifier="rolepart-bdff3654-cbb3-bdf9-063a-9d35f619a986">
          <imsld:title>Role Part</imsld:title>
          <imsld:role-ref ref="role-79d92c95-7ec4-0cc0-711a-963a476e2a63" />
          <imsld:support-activity-ref ref="sa-af662654-3139-7f1d-84d1-8726d7aeb5f6" />
        </imsld:role-part>
      </imsld:act>
    </imsld:play>
  </imsld:method>
</imsld:learning-design>
  </organizations>
  <resources>
<resource identifier="resource-44329a20-40df-d925-a31e-2579f707155d" type="webcontent" href="www.la2.com" />
<resource identifier="resource-c7f25198-8dce-d046-4594-7a2b22b5dd15" type="webcontent" href="www.alumno3.com" />
<resource identifier="resource-f6455607-038b-f81a-b59e-98c9e395eeb9" type="webcontent" href="www.la1.com" />
<resource identifier="resource-37f8f529-49c1-552c-e0a4-8aa581d93aa5" type="webcontent" href="www.profesor2.com" />
<resource identifier="resource-58164a05-d1a7-73a5-2fe9-5b3a0a9522be" type="webcontent" href="www.pruebaoverview.com" />
<resource identifier="resource-f8de38c1-d7f8-a9a5-b7a4-c737d967daf1" type="webcontent" href="www.profesor1.com" />
<resource identifier="resource-51088ea6-51d4-7e18-f62b-76cbcb650329" type="webcontent" href="www.sa4.com" />
<resource identifier="resource-8e804d72-e9aa-51ff-3e4a-c00a2e731aaa" type="webcontent" href="www.sa3.com" />
<resource identifier="resource-f15c67c4-7a03-8ff7-0bd2-541a1859e985" type="webcontent" href="www.sa2.com" />
<resource identifier="resource-d54d1b13-cb38-f478-a40e-bbe0d950357a" type="webcontent" href="www.LA3.com" />
<resource identifier="resource-961c7c0a-8ca4-8c20-aea2-65439c02941e" type="webcontent" href="www.sa1.com" />
  </resources>
</manifest>

我在访问所有节点和属性时遇到麻烦,我试图使用DOM和xmlsimple,但是在xml的其他部分不混合的情况下无法正确访问所有数据

使用此代码,我设法获得一些信息

<?PHP

$xmlDoc = new DOMDocument();
$xmlDoc->load("completo.xml");

$x = $xmlDoc->documentElement;
$xpath= new DOMXPath($xmlDoc);

$x = $xmlDoc->getElementsByTagName( "organizations" );
foreach( $x as $x )
{
$learningdesigns = $x->getElementsByTagName( "title" );
$learningdesign = $learningdesigns->item(0)->nodeValue;
 echo "Learning-desgin = "."$learningdesign";
print "<br>";
$learningobjectives = $x->getElementsByTagName( "title" );
$learningobjective = $learningobjectives->item(1)->nodeValue;
 echo "Learning-objective = "."$learningobjective";
print "<br>";
$items = $x->getElementsByTagName( "title" );
$item = $items->item(2)->nodeValue;
 echo "item = "."$item";
print "<br>";
}
echo "separacion -------------------------";print "<br>";


$ld = $xmlDoc->getElementsByTagName( "learning-objectives" );

foreach( $ld as $ld ) 
{ 
    $identifierref = $ld->getAttribute('identifierref'); 

    $xmltitle = $ld->getElementsByTagName( "title" ); 
    $xmltitles= $xmltitle->item(0)->nodeValue; 

    $xmlitem = $ld->getElementsByTagName( "title" ); 
    $xmlitems = $xmlitem->item(0)->nodeValue; 
    
    echo "$xmltitles - $xmlitems\n"; 
} 

?>

那么关于如何解决此问题的任何想法?

1 个答案:

答案 0 :(得分:0)

我最终几乎使用了XPath。如果有人正在寻找类似的东西,这是一个例子:

$xmlDoc = new DOMDocument();
$xmlDoc->load("imsld.xml");

$x = $xmlDoc->documentElement;
$xpath= new DOMXPath($xmlDoc);

$nodelist = $xpath->query("//imsld:learning-design/imsld:prerequisites/imsld:title");
print  "prerequisitos = ".$nodelist->item($nodelist->length - 1)->textContent."\n";

for ($i = 1; $i < $lds; $i++)
{
$asd = $xpath->query("//imsld:learning-design/imsld:components/imsld:activities/imsld:learning-activity[".$i."]/imsld:learning-objectives/imsld:title");
print  $asd->item($asd->length - 1)->textContent."\n";
print "<br>";
}