【问题标题】:Why duplicate element is being appended in XML?为什么在 XML 中附加重复元素?
【发布时间】:2019-09-26 07:28:11
【问题描述】:

我正在使用 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>

以下是我用来创建此 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 xmldom xerces-c


    【解决方案1】:

    我建议你:

    用正确的术语说话:内存中的 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 \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
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多