how to write a CDATA node using libxml2?

后端 未结 2 1915
忘掉有多难
忘掉有多难 2021-01-19 23:19

I\'m using libxml2 to read/write xml files. Now I\'m trying to write a CDATA node.

Here is what I tried:

nodePtr = xmlNewChild( parentPtr, NULL, \"f         


        
2条回答
  •  深忆病人
    2021-01-19 23:59

    Figured it out. The trick is in knowing that CDATA text content is actually a child and not a part of the current node, and the critical API to call is xmlNewCDataBlock(). Using the same example as above:

    nodePtr = xmlNewChild( parentPtr, NULL, "foo", NULL );
    cdataPtr = xmlNewCDataBlock( doc, "Testing 1 < 2", 13 );
    xmlAddChild( nodePtr, cdataPtr );
    

    This will produce the following xml:

    
    

提交回复
热议问题