1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "libxml_utils.h"
7 #include "libxml/xmlreader.h"
9 std::string
XmlStringToStdString(const xmlChar
* xmlstring
) {
10 // xmlChar*s are UTF-8, so this cast is safe.
12 return std::string(reinterpret_cast<const char*>(xmlstring
));
17 XmlReader::XmlReader() : reader_(NULL
) {
20 XmlReader::~XmlReader() {
22 xmlFreeTextReader(reader_
);
25 bool XmlReader::Load(const std::string
& input
) {
26 const int kParseOptions
= XML_PARSE_RECOVER
| // recover on errors
27 XML_PARSE_NONET
; // forbid network access
28 // TODO(evanm): Verify it's OK to pass NULL for the URL and encoding.
29 // The libxml code allows for these, but it's unclear what effect is has.
30 reader_
= xmlReaderForMemory(input
.data(), static_cast<int>(input
.size()),
31 NULL
, NULL
, kParseOptions
);
32 return reader_
!= NULL
;
35 bool XmlReader::LoadFile(const std::string
& file_path
) {
36 const int kParseOptions
= XML_PARSE_RECOVER
| // recover on errors
37 XML_PARSE_NONET
; // forbid network access
38 reader_
= xmlReaderForFile(file_path
.c_str(), NULL
, kParseOptions
);
39 return reader_
!= NULL
;
42 bool XmlReader::NodeAttribute(const char* name
, std::string
* out
) {
43 xmlChar
* value
= xmlTextReaderGetAttribute(reader_
, BAD_CAST name
);
46 *out
= XmlStringToStdString(value
);
51 bool XmlReader::IsClosingElement() {
52 return NodeType() == XML_READER_TYPE_END_ELEMENT
;
55 bool XmlReader::ReadElementContent(std::string
* content
) {
56 const int start_depth
= Depth();
58 if (xmlTextReaderIsEmptyElement(reader_
)) {
59 // Empty tag. We succesfully read the content, but it's
62 // Advance past this empty tag.
68 // Advance past opening element tag.
72 // Read the content. We read up until we hit a closing tag at the
73 // same level as our starting point.
74 while (NodeType() != XML_READER_TYPE_END_ELEMENT
|| Depth() != start_depth
) {
75 *content
+= XmlStringToStdString(xmlTextReaderConstValue(reader_
));
80 // Advance past ending element tag.
87 bool XmlReader::SkipToElement() {
90 case XML_READER_TYPE_ELEMENT
:
92 case XML_READER_TYPE_END_ELEMENT
:
95 // Skip all other node types.
103 // XmlWriter functions
105 XmlWriter::XmlWriter()
109 XmlWriter::~XmlWriter() {
111 xmlFreeTextWriter(writer_
);
113 xmlBufferFree(buffer_
);
116 void XmlWriter::StartWriting() {
117 buffer_
= xmlBufferCreate();
118 writer_
= xmlNewTextWriterMemory(buffer_
, 0);
119 xmlTextWriterSetIndent(writer_
, 1);
120 xmlTextWriterStartDocument(writer_
, NULL
, NULL
, NULL
);
123 void XmlWriter::StopWriting() {
124 xmlTextWriterEndDocument(writer_
);
125 xmlFreeTextWriter(writer_
);