2017-04-11 56 views
0

這裏我「C1.xml」文件:Matlab:讀取多個孩子的XML?

<!DOCTYPE GroundTruth> 
<dataset name="camera1"> 
<frame number="00000000"/> 
<frame number="00000001"/> 
<frame number="00000002"/> 
<frame number="00000003"/> 
<frame number="00000004"/> 
. 
. 
. 
<frame number="00000233"> 
    <person id="0001"> 
    <leftEye x="626" y="210"/> 
    <rightEye x="643" y="214"/> 
    </person> 
</frame> 
<frame number="00000234"> 
    <person id="0003"> 
    <leftEye x="619" y="211"/> 
    <rightEye x="636" y="215"/> 
    </person> 
</frame> 
<frame number="00000235"> 
    <person id="0025"> 
    <leftEye x="613" y="213"/> 
    <rightEye x="630" y="216"/> 
    </person> 
</frame> 
<frame number="00004403"/> 
<frame number="00004404"/> 
<frame number="00004405"/> 
<frame number="00004406"/> 
<frame number="00004407"/> 
<frame number="00004408"/> 
<frame number="00004409"/> 
</dataset> 

我的不完整代碼:

xDoc = xmlread('C1.xml'); 

allListitems = xDoc.getElementsByTagName('frame'); 

frameL = allListitems.getLength; 

for k = 0:frameL-1 
    thisElement = allListitems.item(k); 
    thisList = thisElement.getElementsByTagName('person'); 
    if (thisList.getLength~=0) 
     thisElement = thisList.item(0); 
     % problem is here where I also don't know what type of data findCbk 
     % have (char or ...??) 

     findCbk = char(thisElement.getFirstChild.getData); 

    end 
end 

我希望得到相應的人ID的幀數和左眼和右眼的數據。所以,如果我sprintf findCbk,它會給我號碼,我不能涉及%d和空的%s

回答

0

所以這裏的答案。我需要的是getAttribute。所以如果有人遇到同樣的問題,或者如果有人能糾正我的錯誤,我會寫下代碼。

xDoc = xmlread('C1.xml'); 
allListitems = xDoc.getElementsByTagName('frame'); 

frameL = allListitems.getLength; 

for k = 0: frameL-1 
    thisElement = allListitems.item(k); 
    frameNum = (char(thisElement.getAttribute('number'))) 

%check the child inside element frame 
thisList = thisElement.getElementsByTagName('person'); 
face = thisList.getLength; 
if (face~=0) 
    thisElement = thisList.item(0); 
    personID = (char(thisElement.getAttribute('id'))) 
     eyeL = thisElement.getElementsByTagName('leftEye'); 
         thisElementl = eyeL.item(0); 
       xL = (char(thisElementl.getAttribute('x'))) 
       yL = (char(thisElementl.getAttribute('y'))) 

     eyeR = thisElement.getElementsByTagName('rightEye'); 
         thisElementr = eyeR.item(0); 
       xR = (char(thisElementr.getAttribute('x'))) 
       yR = (char(thisElementr.getAttribute('y'))) 
    break 
end 
end 

它將返回:

frameNum = 

00000000 

frameNum = 

00000001  

frameNum = 

00000002 
. 
. 
. 
.  
frameNum = 

00000230 

frameNum = 

00000231 

frameNum = 

00000232 

frameNum = 

00000233 

personID = 

0003 

xL = 

626  

yL = 

210 

xR = 

643 

yR = 

214