如何使用libxml2的xmltextwriter编写以下XML内容?

时间:2013-09-01 15:34:44

标签: libxml2

我正在尝试xxxxx,但面临xxxxxxxxx的问题。我已经在互联网上搜索但没有找到任何解决方案。主要是我想使用xmltextwriter将命名空间写为URL。内容应如下所示:

**<!-- language-all: lang-xml -->

    <?xml version="1.0" encoding="UTF-8"?>
    <book
        xmlns="http://schemas.dmtf.org/ovf/environment/1"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xmlns:oe="http://schemas.dmtf.org/ovf/environment/1"
        oe:id="">
    </book>
  **

代码就在这里,

int main()
{
  XmlWriter testWriter;
  testWriter.StartWriting("test.xml");
  testWriter.StartDocument("1.0","UTF-8");

  testWriter.StartElement("book");

  testWriter.StartIndenting();

  testWriter.AddAttributeNS("xmlns", "http://schemas.dmtf.org/ovf/environment/1", "", "http://schemas.dmtf.org/ovf/environment/1");
  testWriter.AddAttributeNS("xmlns", "http://www.w3.org/2001/XMLSchema-instance", "xsi", "http://www.w3.org/2001/XMLSchema-instance");
  testWriter.AddAttributeNS("xmlns", "http://schemas.dmtf.org/ovf/environment/1", "oe", "http://www.w3.org/2001/XMLSchema-instance");
  testWriter.AddAttributeNS("oe", ":", "id", "");

  testWriter.EndElement();
  testWriter.StopWriting();

  std::string buf;
  testWriter.xml_to_string(buf);
  return 0;
}

bool StartElement(const std::string& element_name) {
       return xmlTextWriterStartElement(writer_,
                                     BAD_CAST element_name.c_str()) >= 0;
}

bool XmlWriter::AddAttributeNS(const std::string& name,
                        const std::string& value,
                        const std::string& prefix,
                        const std::string& namespaceURI )
{

      if(namespaceURI != "")
         return xmlTextWriterWriteAttributeNS (writer_,
                                            BAD_CAST prefix.c_str(),
                                            BAD_CAST name.c_str(),
                                            BAD_CAST namespaceURI.c_str(),
                                            BAD_CAST value.c_str()) >= 0;
       else
         return xmlTextWriterWriteAttributeNS (writer_,
                                            BAD_CAST prefix.c_str(),
                                            BAD_CAST name.c_str(),
                                            NULL,
                                            BAD_CAST value.c_str()) >= 0;


}

0 个答案:

没有答案