xml.libxml2_添加带tagname的xml文本(xmlNewTextChild)

1、

2、例子代码:

int TgText::NodeNew_G2SVG(xmlNode* _pNodeCurrent_G, xmlNode* _pNodeParent_SVG, xmlNode** _ppNodeNew_SVG)
{
    //// ZC: SVG 方式
    //*_ppNodeNew_SVG = ChildNodeCreate(_pNodeParent_SVG, "text");


    // ZC: 内嵌HTML方式
    xmlNode* pNode_switch = ChildNodeCreate(_pNodeParent_SVG, "switch");
    xmlNode* pNode_foreignObject = ChildNodeCreate(pNode_switch, "foreignObject");
    //xmlSetProp(pNode_foreignObject, BAD_CAST "x", BAD_CAST "");
    //xmlSetProp(pNode_foreignObject, BAD_CAST "y", BAD_CAST "");
    xmlSetProp(pNode_foreignObject, BAD_CAST "width",  BAD_CAST TextWidth_switch_foreignObject);
    xmlSetProp(pNode_foreignObject, BAD_CAST "height", BAD_CAST TextHeight_switch_foreignObject);

    xmlNode* pNode_p = ChildNodeCreate(pNode_foreignObject, "p");
    xmlSetProp(pNode_p, BAD_CAST "xmlns", BAD_CAST "http://www.w3.org/1999/xhtml");

    {
        // <switch/>下面的<text/>
        xmlNode* pNode_text = ChildNodeCreate(pNode_switch, "text");
        xmlNode* pContent = xmlNewText(BAD_CAST "<p>Your SVG viewer cannot display html.</p>");
        xmlAddChild(pNode_text, pContent);
        
        //xmlNewDocRawNode(pNode_text->doc, NULL, BAD_CAST "p", BAD_CAST "Your SVG viewer cannot display html.");
        xmlNode* p = xmlNewTextChild(pNode_text, NULL, BAD_CAST "p", BAD_CAST "Your SVG viewer cannot display html.");
        xmlSetProp(p, BAD_CAST "font-size", BAD_CAST "16px");
    }

    *_ppNodeNew_SVG = pNode_p;

    return 0;
}

  2.1、上面的代码,主要的目的是 在 <text/>下添加节点内容 “<p>Your SVG viewer cannot display html.</p>”,效果如下:

<text>&lt;p&gt;Your SVG viewer cannot display html.&lt;/p&gt;<p font-size="16px">Your SVG viewer cannot display html.</p></text>

   (1)、但是 函数xmlNewText() 会将 尖括号 翻译成 “&lt;”和“&gt;”

   (2)、xmlNewTextChild() 却可以 达到这个效果,还能对 返回的节点指针进行 属性操作

3、

4、

5、

猜你喜欢

转载自www.cnblogs.com/cppskill/p/9919251.html
xml