2016-09-19 11 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子の 'identifier'属性からの値とassessmentSectionサブchildの 'identifier'属性から値を取得することです。私はすでに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

それは働いて、ありがとう! {["TesBagian-1":{["evaluationvalue-1"]、["evaluationvalue-2]}]、[" TesBagian-2 "]のような、array内のtestPart属性の一部になります。 :{["evaluationvalue-1"]}]}? –

関連する問題