我正在尝试按如下方式创建 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>