2014-01-29 68 views
0

我嘗試使用libxml庫C/C++創建簡單的XML文件。使用libxml將C/C++添加到子元素的命名空間不是根元素

#include <libxml/tree.h> 

int main() 
{ 

xmlDocPtr doc = xmlNewDoc(BAD_CAST "1.0"); 
xmlNodePtr root = xmlNewNode(NULL,BAD_CAST "BookingUpdate"); 

xmlDocSetRootElement(doc,root); 
//Create new Namespaces 
xmlNsPtr ns = xmlNewNs (root, BAD_CAST"http://www.systems.com/XMLSchema/BUM/TriggerBookingUpdate/2_0", BAD_CAST ""); 
xmlNsPtr ns1 = xmlNewNs (root, BAD_CAST"http://www.systems.com/XMLSchema/common/header/1_1", BAD_CAST "ns1"); 
xmlNsPtr xsi = xmlNewNs (root, BAD_CAST"http://www.w3.org/2001/XMLSchema-instance", BAD_CAST "xsi"); 

//Set the new namespace on root 
xmlSetNs(root,ns1); 

// Create a new Element 
xmlNewChild(root,NULL,BAD_CAST"InterfaceHeader", BAD_CAST"Value"); 


xmlDocFormatDump(stdout,doc,1); 
xmlFreeDoc(doc); 
return 0; 
} 

結果:根元素命名空間創建

<?xml version="1.0"?> 
<ns1:BookingUpdate 
    xmlns:="http://www.systems.com/XMLSchema/BUM/TriggerBookingUpdate/2_0" 
    xmlns:ns1="http://www.systems.com/XMLSchema/common/header/1_1" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> 
    <ns1:InterfaceHeader> 
      Value 
     </ns1:InterfaceHeader> 
</ns1:BookingUpdate> 

我希望有一個結果根元素沒有任何名稱空間,但只有子元素與相應的命名空間

<?xml version="1.0"?> 
<BookingUpdate xmlns:="http://www.systems.com/XMLSchema/BUM/TriggerBookingUpdate/2_0" xmlns:ns1="http://www.systems.com/XMLSchema/common/header/1_1" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> 
     <ns1:InterfaceHeader> 
      Value 
     </ns1:InterfaceHeader> 
</BookingUpdate> 

我怎樣才能得到這個?

謝謝

回答

0

我已經找到了解決自己

#include <libxml/tree.h> 

int main() 
{ 
xmlNodePtr child1; 
xmlDocPtr doc = xmlNewDoc(BAD_CAST "1.0"); 
xmlNodePtr root = xmlNewNode(NULL,BAD_CAST "BookingUpdate"); 

xmlDocSetRootElement(doc,root); 
//Create namespaces 
xmlNsPtr ns = xmlNewNs (root, BAD_CAST"http://www.systems.com/XMLSchema/BUM/TriggerBookingUpdate/2_0", BAD_CAST ""); 
xmlNsPtr ns1 = xmlNewNs (root, BAD_CAST"http://www.systems.com/XMLSchema/common/header/1_1", BAD_CAST "ns1"); 
xmlNsPtr xsi = xmlNewNs (root, BAD_CAST"http://www.w3.org/2001/XMLSchema-instance", BAD_CAST "xsi"); 

//Set namespace on root 
//xmlSetNs(root,ns1); 

// Create a new Element 
child1=xmlNewChild(root,NULL,BAD_CAST"InterfaceHeader", BAD_CAST"Value"); 
xmlSetNs(child1, ns1); 

xmlDocFormatDump(stdout,doc,1); 
xmlFreeDoc(doc); 
return 0; 
} 
相關問題