2016-09-19 132 views
-1
<?xml version="1.0" encoding="UTF-8"?> 
<assessmentTest xmlns="http://www.imsglobal.org/xsd/imsqti_v2p1" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" identifier="Tes-1" title="Tes 1" toolName="tao" toolVersion="2.6.7" xsi:schemaLocation="http://www.imsglobal.org/xsd/imsqti_v2p1 http://www.imsglobal.org/xsd/qti/qtiv2p1/imsqti_v2p1.xsd"> 
    <timeLimits maxTime="180" allowLateSubmission="false"/> 
    <testPart identifier="TesBagian-1" navigationMode="linear" submissionMode="individual"> 
    <itemSessionControl maxAttempts="0" showFeedback="false" allowReview="true" showSolution="false" allowComment="false" allowSkipping="true" validateResponses="false"/> 
    <timeLimits maxTime="180" allowLateSubmission="false"/> 
    <assessmentSection identifier="subBagian-1" required="true" fixed="false" title="Sub Bagian 1" visible="true" keepTogether="true"> 
     <assessmentItemRef identifier="item-1" required="false" fixed="false" href="http://localhost/tao_dev/icat.rdf#i147252537452611434"/> 
    </assessmentSection> 
    <assessmentSection identifier="assessmentSection-1" required="false" fixed="false" title="Sub Bagian 2" visible="false" keepTogether="true"/> 
    </testPart> 
    <testPart identifier="TesBagian-2" navigationMode="linear" submissionMode="individual"> 
    <assessmentSection identifier="subBagian-2" required="false" fixed="false" title="Sub Bagian 1" visible="false" keepTogether="true"/> 
    <assessmentSection identifier="assessmentSection-2" required="false" fixed="false" title="Sub Bagian 2" visible="false" keepTogether="true"/> 
    </testPart> 
</assessmentTest> 

所以我想要做的是從testPart子的「標識符」屬性和assessmentSection子標識符的「標識符」屬性中獲取值。我已經加載了xml文件,但我不知道該怎麼做。如何從這個XML元素獲取屬性?

+0

的SimpleXML搖鈴? –

+0

請告訴我們您的來源。 – ThW

回答

1

您沒有指定如何加載xml,也沒有顯示任何代碼,但是下面的內容應該給你一個關於如何繼續的想法。

$xml=' 
    <?xml version="1.0" encoding="UTF-8"?> 
    <assessmentTest xmlns="http://www.imsglobal.org/xsd/imsqti_v2p1" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" identifier="Tes-1" title="Tes 1" toolName="tao" toolVersion="2.6.7" xsi:schemaLocation="http://www.imsglobal.org/xsd/imsqti_v2p1 http://www.imsglobal.org/xsd/qti/qtiv2p1/imsqti_v2p1.xsd"> 
     <timeLimits maxTime="180" allowLateSubmission="false"/> 
     <testPart identifier="TesBagian-1" navigationMode="linear" submissionMode="individual"> 
     <itemSessionControl maxAttempts="0" showFeedback="false" allowReview="true" showSolution="false" allowComment="false" allowSkipping="true" validateResponses="false"/> 
     <timeLimits maxTime="180" allowLateSubmission="false"/> 
     <assessmentSection identifier="subBagian-1" required="true" fixed="false" title="Sub Bagian 1" visible="true" keepTogether="true"> 
      <assessmentItemRef identifier="item-1" required="false" fixed="false" href="http://localhost/tao_dev/icat.rdf#i147252537452611434"/> 
     </assessmentSection> 
     <assessmentSection identifier="assessmentSection-1" required="false" fixed="false" title="Sub Bagian 2" visible="false" keepTogether="true"/> 
     </testPart> 
     <testPart identifier="TesBagian-2" navigationMode="linear" submissionMode="individual"> 
     <assessmentSection identifier="subBagian-2" required="false" fixed="false" title="Sub Bagian 1" visible="false" keepTogether="true"/> 
     <assessmentSection identifier="assessmentSection-2" required="false" fixed="false" title="Sub Bagian 2" visible="false" keepTogether="true"/> 
     </testPart> 
    </assessmentTest>'; 



libxml_use_internal_errors(true); 
$dom=new DOMDocument; 
$dom->validateOnParse=false; 
$dom->standalone=true; 
$dom->strictErrorChecking=false; 
$dom->recover=true; 
$dom->formatOutput=false; 
$dom->loadXML($xml); 
libxml_clear_errors();         

$colParts=$dom->getElementsByTagName('testPart'); 
$colSects=$dom->getElementsByTagName('assessmentSection'); 


$data=array(); 
foreach($colParts as $node){ 
    if($node->nodeType==XML_ELEMENT_NODE && $node->hasAttribute('identifier')) $data['testPart'][]=$node->getAttribute('identifier'); 
} 
foreach($colSects as $node){ 
    if($node->nodeType==XML_ELEMENT_NODE && $node->hasAttribute('identifier')) $data['assessmentSection'][]=$node->getAttribute('identifier'); 
} 

print_r($data); 

不知道這或多或少你與你的後續問題意味着什麼,但應該給你一個良好的開端,如果需要細化。

$colParts=$dom->getElementsByTagName('testPart'); 
foreach($colParts as $node){ 
    if($node->nodeType==XML_ELEMENT_NODE && $node->hasAttribute('identifier')) { 
     $attrib=$node->getAttribute('identifier'); 
     $data['testPart'][ $attrib ]=array(); 
    } 
    if($node->hasChildNodes()){ 
     foreach($node->childNodes as $child){ 
      if($child->tagName=='assessmentSection'){ 
       $data['testPart'][ $attrib ][]=$child->getAttribute('identifier'); 
      } 
     } 
    } 
} 

print_r($data); 

將輸出這樣的:

/* 
    Array 
    (
     [testPart] => Array 
      (
       [TesBagian-1] => Array 
        (
         [0] => subBagian-1 
         [1] => assessmentSection-1 
        ) 

       [TesBagian-2] => Array 
        (
         [0] => subBagian-2 
         [1] => assessmentSection-2 
        ) 

      ) 

    ) 

*/ 
+0

它的工作,謝謝!任何想法如何使assessmentSection值成爲數組中testPart屬性的一部分,如{[「TesBagian-1」:{[「assessmentvalue-1」],[「assessmentvalue-2]}],[」TesBagian-2「 :{[「assessmentvalue-1」]}]}? –