为什么要在 XML 中附加重复元素?

  • 本文关键字:元素 XML xml xmldom xerces-c
  • 更新时间 :
  • 英文 :


我正在使用Xerces API在内存中创建XML。我能够将重复的元素(相同的元素标签名称和属性(附加到元素节点。请建议我,如果XML元素已经作为元素节点的子级存在,我如何避免附加XML元素。

以下是创建的 XML(标记名称为"Batch"的两个元素重复(:

<?xml version="1.0" encoding="UTF-8" standalone="no" ?>
<root>Text_root_01
<Student>Text_Student_01
<Batch ID="B01" Subject="Math">Ratnesh</Batch>Text_Student_02
<Batch ID="B01" Subject="Math">Ratnesh</Batch>Text_Student_03
</Student>Text_root_02
<Othres Company="Airtel" NoOfEmployee="15">Text_Others</Othres>Text_root_03
</root>

Follwing是我用来创建这个XML的CPP代码:

int main(int argc, char *argv[]) {
//XMLCh temp_buff[64];
XMLPlatformUtils::Initialize();
DOMImplementation *p_domImpl = DOMImplementationRegistry::getDOMImplementation(XMLString::transcode("Core"));
DOMDocument *p_domDoc = p_domImpl->createDocument(0, XMLString::transcode("root"), 0);
DOMElement *p_root_elem = p_domDoc->getDocumentElement();
DOMText *root_text_01 = p_domDoc->createTextNode(XMLString::transcode("Text_root_01"));
p_root_elem->appendChild(root_text_01);
DOMElement *root_child_student = p_domDoc->createElement(XMLString::transcode("Student"));
p_root_elem->appendChild(root_child_student);
DOMText* student_text_01 = p_domDoc->createTextNode(XMLString::transcode("Text_Student_01"));
root_child_student->appendChild(student_text_01);

DOMElement* student_child_batch = p_domDoc->createElement(XMLString::transcode("Batch"));
student_child_batch->setAttribute(XMLString::transcode("Subject"), XMLString::transcode("Math"));
student_child_batch->setAttribute(XMLString::transcode("ID"), XMLString::transcode("B01"));
DOMText* batch_text = p_domDoc->createTextNode(XMLString::transcode("Ratnesh"));
student_child_batch->appendChild(batch_text);
root_child_student->appendChild(student_child_batch);
DOMText* student_text_02 = p_domDoc->createTextNode(XMLString::transcode("Text_Student_02"));
root_child_student->appendChild(student_text_02);
student_child_batch = p_domDoc->createElement(XMLString::transcode("Batch"));
student_child_batch->setAttribute(XMLString::transcode("Subject"), XMLString::transcode("Math"));
student_child_batch->setAttribute(XMLString::transcode("ID"), XMLString::transcode("B01"));
batch_text = p_domDoc->createTextNode(XMLString::transcode("Ratnesh"));
student_child_batch->appendChild(batch_text);
root_child_student->appendChild(student_child_batch);

DOMText* student_text_03 = p_domDoc->createTextNode(XMLString::transcode("Text_Student_03"));
root_child_student->appendChild(student_text_03);
DOMText* root_text_02 = p_domDoc->createTextNode(XMLString::transcode("Text_root_02"));
p_root_elem->appendChild(root_text_02);
DOMElement *root_child_others = p_domDoc->createElement(XMLString::transcode("Othres"));
root_child_others->setAttribute(XMLString::transcode("Company"), XMLString::transcode("Airtel"));
root_child_others->setAttribute(XMLString::transcode("NoOfEmployee"), XMLString::transcode("15"));
p_root_elem->appendChild(root_child_others);
DOMText* others_text_01 = p_domDoc->createTextNode(XMLString::transcode("Text_Others"));
root_child_others->appendChild(others_text_01);
DOMText* root_text_03 = p_domDoc->createTextNode(XMLString::transcode("Text_root_03"));
p_root_elem->appendChild(root_text_03);

xml_output_to_stream(p_domDoc);
p_domDoc->release();
XMLPlatformUtils::Terminate();
return 0;
}

我建议你:

用正确的术语说话:内存中的XML称为DOM(文档对象模型(
XML是文本文件中的最终文本表示形式。
构建一个对 XML 更友好的结构:
- 在批处理中删除名称,这是学生的名字(我猜(并将其放入学生标签中。
- 不要在标签之间喷涂文本,使用标签(或<等(,并且只在里面放文本。>
神奇之处在于解耦功能。

<root>
<text>text_root1</text>
<Student name="Ratnesh">
<text>Text_Student_01</text>
<Batch ID="B01" Subject="Math"></Batch>
<Batch ID="B02" Subject="Geology"></Batch>
</Student>
<Student name="Mike">
<text>Text_Student_02</text>
<Batch ID="B01" Subject="Math"></Batch>
<Batch ID="B03" Subject="Sport"></Batch>
</Student>

<Others Company="Airtel" NoOfEmployee="15">
<text>Text for others...</text>
</Others>

然后你可以构建方法或函数

Method (or function) insertStudent((NODE)parent, (string)text, (string)name) {
> create a StudentNode <STUDENT> 
> put attribute name="name" into StudenNode
> create <TEXT> node
> put sting(text) into <TEXT> node
> put <TEXT>node into StudentNode
> return StudentNode
}


Method (or function) insertBatch (StudentNode, batchId, batchName, (string) text) {
> create a node <NodeBatch>
> put him attribute Subject=string batchName
> put him  attribute batchId=batchId
> create a <TEXT> node, put him (string) text
> put <TEXT> node in <NodeBatch>
}


Method (or function) searchStudent  ((string)StudentName) {
> use XPATH ROOTStudent[name="StudentName"]
> return a List of Node , or empty node if not found
}


Method (or function) searchBatchByName (NodeStudent, (string)batchName) {
>  use XPATH Batch[subject="batchName"] starting at node NodeStudent
>  return a List of Node , or empty node if not found
}

Method (or function) searchBatchById (nodeStudent, (string)batchId ) {
>  use XPATH Batch[id="batchId"] starting at node nodeStudent
>  return a List of Node , or empty node if not found
}

Method (or function) insertActivity ((string)studentName,(string)textStudent, (string)batchName , (string|int) batchID, (string)textBatch) {
list = searchStudent (studentName)
if (list).len>0 {
nodeStudent=list[0]
} else {
nodeStudent=insertStudent(ROOT,textStudent,studentName)
}
listBatch=searchBatchByName(nodeStudent,batchName)) 
*** OR list=searchBatchById (nodeStudent,batchId)) *** 
if (listBatch.len>0) { skip } // already exist, do nothing
else {
insertBatch(listBatch[0],batchId, batchName, textBash)
}
}

并测试

insertActivity ("Ratnesh","text student","Math","B01","text_bash")
insertActivity ("Mike","text student","Sport","B03","text_bash")   
insertActivity ("Ratnesh","text student","Geology","B02","text_bash")   
insertActivity ("Mike","text student","Sport","B03","text_bash")   // oops duplicate entry

最新更新