Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
为什么要在XML中追加重复元素?_Xml_Xmldom_Xerces C - Fatal编程技术网

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

为什么要在XML中追加重复元素?,xml,xmldom,xerces-c,Xml,Xmldom,Xerces C,我正在使用Xerces API创建内存中的XML。我能够将重复的元素(相同的元素标记名和属性)附加到元素节点。请建议我,如果XML元素已经作为元素节点的子元素存在,如何避免附加XML元素 以下是创建的XML(标记名为“Batch”的两个元素是重复的): 我建议你: 使用正确的术语:内存中的XML称为DOM(文档对象模型) XML是文本文件中的最终文本表示形式。 构建更加xml友好的结构: -批量删除name,这是学生的名字(我猜),并将其放入学生标签中。 -不要在标签之间喷洒文字,使用标签(或

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

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

我建议你:

使用正确的术语:内存中的XML称为DOM(文档对象模型)
XML是文本文件中的最终文本表示形式。
构建更加xml友好的结构:
-批量删除name,这是学生的名字(我猜),并将其放入学生标签中。
-不要在标签之间喷洒文字,使用标签(或
<?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>
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;

}
<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 \ROOT\Student[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