最近在开发iOS程序,调用WebService时候使用到了iOS内置的libxml2来作为DOM树的操作库,按照官方代码的参考,写了一段创建DOM的代码,部分片段如下:
xmlNodePtr generateSoap11MessageWrapper(void)
{
// create Document with Default version
const xmlDocPtr pDoc = xmlNewDoc(NULL);
{
// force version and encoding
pDoc->version = _XS"1.0";
pDoc->encoding = _XS"UTF-8";
}
// create Root-Element
const xmlNodePtr pRoot = xmlNewNode(NULL, _XS"Envelope");
{
// attach namespace definition to @pRoot->nsDef.
xmlNewNs(pRoot, _XS"http://www.w3.org/2001/XMLSchema-instance", _XS"xsi");
xmlNewNs(pRoot, _XS"http://www.w3.org/2001/XMLSchema", _XS"xsd");
const xmlNsPtr pNsSoap = xmlNewNs(pRoot, _XS"http://schemas.xmlsoap.org/soap/envelope/", _XS"soap");
// set the ns as the node namespace
xmlSetNs(pRoot, pNsSoap);
}
// set @pRoot as Document's roo