2012-11-13 2 views
0

QXmlStreamWriter를 사용하여 xml 파일을 생성합니다. 이 파일은 다음과 같습니다 QDomDocument를 사용하여 xml 파일을 읽는 것만으로 첫 번째 줄을 얻습니다.

<?xml version="1.0" encoding="UTF-8"?> 
<pedestrianinfo> 
    <pedestrian uuid="2112e2ed-fc9b-41e8-bbcb-b44ad78bde11"> 
     <module>11.1208</module> 
     <direction>4</direction> 
     <row>5</row> 
     <column>71</column> 
    </pedestrian> 
    <pedestrian uuid="1aabb9c1-4aa7-4f47-9542-36d2dfaa26e4"> 
     <module>1.48032</module> 
     <direction>4</direction> 
     <row>67</row> 
     <column>31</column> 
    </pedestrian> 

... 

</pedestrianinfo> 

가 그럼 난 QDomDocument으로 내용을 읽어보십시오. 내 코드는 다음과 같습니다 : 여기

xmlReader *xp = new xmlReader(QString("D:\\0T.xml")); 
    if(xp->openFile()) { 
     if(xp->isGetRootIndex()) { 
      xp->parseRootIndexElement(); 
     } 
     else 
      cout<<"Unable to get root index."<<endl; 
    } 

isGetRootIndex()이다 :

bool xmlReader::isGetRootIndex() 
{ 
    doc.setContent(&file,false); 
    root = doc.documentElement(); 
    if(root.tagName() == getRootIndex()) //rootIndex=="pedestrianinfo" 
     return true; 
    return false; 
} 

이 parseRootIndexElement()이다 :

void xmlReader::parseRootIndexElement() 
{ 
    QDomNode child = root.firstChild(); 
    while(!child.isNull()) { 
     if(child.toElement().tagName() == getTagNameP()) //"childTagName=="pedestrian" 
      parseEntryElement(child.toElement()); 
     qDebug()<<"module="<<module<<" direction="<<direction<<" row="<<row<<" column="<<column; 
     child = child.nextSibling(); 
    } 
} 
parseEntryElement이 (const를 QDomElement & 요소)에 기능입니다

각 태그에서 정보를 얻고 모듈과 같은 변수에 저장하십시오. 그러나 코드를 실행할 때마다 xml 파일의 첫 번째 자식 만 qDebug * ed *가 될 수 있습니다. child.nextSibling()을 실행 한 후 자식이 null이되는 것 같습니다. 왜 다음 보행자 정보를 얻지 못합니까?

답변

0

설명서에 표시된 내용을 기반으로 올바른 모양이됩니다. 아마도 parseEntryElement가 반복자를 예기치 않게 전진시킬 것입니까?

+0

답장을 보내 주셔서 감사합니다. 오늘 나는 내가 만든 끔찍한 실수를 발견했다. 프로그램은 올바른 "오래된"xml 파일을 읽지 않습니다. 죄송합니다. 이 코드는 지금 저에게 효과적입니다. – user957121

관련 문제