2013-05-03 3 views

답변

2

QDomDocument (또는 QXmlStreamWriter)에 XML 스 니펫을 텍스트로 "삽입"할 API가 없습니다. 하나는 API를 사용하고 프로그래밍 방식으로 노드를 만들어야합니다.

+0

Qt DOM에서 이러한 기능을 제공하지 않는다는 것은 슬픈 일입니다. 결국, 당신은 종종 그 일을해야하고 결국 그들은 "완벽한"파서가 이미 쓰여지고 내부에서 접근 할 수있게됩니다. –

1

문자열이 있다고 가정하면, 현재 해결 방법은 DOM 트리를 생성하고 조각에서 트리를 가져온 다음 올바른 위치에 복사하는 것입니다 (가져 오기가 있어야하는 이유가 무엇인지 중간 조각. 당신이 저를 요구하는 경우에 아주 불행한.) 그런 식으로 결과가 태그를 할 필요가 없습니다 있도록 < 텍스트 > 태그를 사용하는 것이

// assuming that 'n' is the node you are replacing or at least inserting after 
// 'parent' is the parent of 'n' 
// 'result' is the string to replace 'n' or insert after 'n' 
QDomDocument doc_text("snap"); 
doc_text.setContent("<text>" + result + "</text>", true, NULL, NULL, NULL); 
QDomDocumentFragment frag(xml.createDocumentFragment()); 
frag.appendChild(xml.importNode(doc_text.documentElement(), true)); 
QDomNodeList children(frag.firstChild().childNodes()); 
const int max(children.size()); 
QDomNode previous(n); 
for(int i(0); i < max; ++i) 
{ 
    QDomNode l(children.at(0)); 
    parent.insertAfter(children.at(0), previous); 
    previous = l; 
} 
// if you are replacing, then delete node n as well 
parent.removeChild(n); 

참고, 그냥 텍스트가 될 수 있으며, 그것은 여전히 ​​작동합니다 .

분명히 다른 문서의 조각 또는 XML이 시작하는 경우 doc_text 개체에 해당 코드를 만드는 코드를 무시하십시오.