我正在尝试创建一个xml内容,如下所示,我在这里使用libxml2和c ++。我在libxml2库api上写了包装器并尝试使用它。但是当我尝试将属性添加到一个时,我遇到了以下错误我的元素。我在谷歌搜索找出类似的问题,但不能。 请帮帮我。 提前谢谢。
<?xml version="1.0" encoding="UTF-8"?>
<Environment>
<PlatformSection>
<Kind>NOVA</Kind>
<Version>2013.1</Version>
<Vendor>Openstack</Vendor>
<Locale>en</Locale>
</PlatformSection>
<PropertySection>
<Property oe:key="com.cisco.csr1000v.mgmt-interface" oe:value="GigabitEthernet1"/>
</PropertySection></Environment>
我的代码如下所示,我使用的是c ++ libxml2 xmltextwriter。
XmlWriter wtr;
wtr.StartWriting("test.xml");
wtr.StartDocument("1.0","UTF-8");
wtr.StartElement("Environment");
wtr.StartIndenting();
wtr.StartElement("PlatformSection");
wtr.WriteElement("Kind","NOVA");
wtr.WriteElement("Version","2013.1");
wtr.WriteElement("Vendor","Openstack");
wtr.WriteElement("Locale","en");
wtr.EndElement();
wtr.StartElement("PropertySection");
wtr.StartElement("Property");
wtr.AddAttribute("oe:key","com.cisco.csr1000v.mgmt-interface");
wtr.AddAttribute("oe:value","GigabitEthernet1");
wtr.EndElement();
wtr.EndElement();
wtr.freeTextWriter();
其中
bool AddAttribute(const std::string& attribute_name,
const std::string& attribute_value) {
return xmlTextWriterWriteAttribute(writer_,
BAD_CAST attribute_name.c_str(),
BAD_CAST attribute_value.c_str()) >= 0;
}
我的o / p给出以下错误,
namespace error : Namespace prefix oe for key on Property is not defined
<Property oe:key="com.cisco.csr1000v.mgmt-interface" oe:value="GigabitEthernet1"
^
namespace error : Namespace prefix oe for value on Property is not defined
<Property oe:key="com.cisco.csr1000v.mgmt-interface" oe:value="GigabitEthernet1"
^
error : xmlTextWriterWriteDocCallback : XML error 201 !
I/O error : flush error
Entity: line 11: parser error : Extra content at the end of the document
</PropertySection>
^
error : xmlTextWriterWriteDocCallback : XML error 5 !
xml is
<?xml version="1.0" encoding="ISO-8859-1"?>
<Environment>
<PlatformSection>
<Kind>NOVA</Kind>
<Version>2013.1</Version>
<Vendor>Openstack</Vendor>
<Locale>en</Locale>
</PlatformSection>
<PropertySection>
<Property key="com.cisco.csr1000v.mgmt-interface" value="GigabitEthernet1"/>
</PropertySection></Environment>