Skip to content

Commit beb16fb

Browse files
Improve README formatting with highlighted blocks (#1055)
Improve README
1 parent 8d8472f commit beb16fb

1 file changed

Lines changed: 121 additions & 106 deletions

File tree

readme.md

Lines changed: 121 additions & 106 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,19 @@
1-
TinyXML-2
2-
=========
1+
# TinyXML-2
32

43
[![Test](https://github.com/leethomason/tinyxml2/actions/workflows/test.yml/badge.svg)](https://github.com/leethomason/tinyxml2/actions/workflows/test.yml)
54

65
TinyXML-2 is a simple, small, efficient, C++ XML parser that can be
76
easily integrated into other programs.
87

9-
The master is hosted on github:
8+
The master is hosted on GitHub:
109
https://github.com/leethomason/tinyxml2
1110

1211
The online HTML version of these docs:
1312
http://leethomason.github.io/tinyxml2/
1413

1514
Examples are in the "related pages" tab of the HTML docs.
1615

17-
What it does.
18-
-------------
16+
## What it does.
1917

2018
In brief, TinyXML-2 parses an XML document, and builds from that a
2119
Document Object Model (DOM) that can be read, modified, and saved.
@@ -37,27 +35,26 @@ code without creating a document first.
3735

3836
TinyXML-2 is designed to be easy and fast to learn. It is one header and
3937
one cpp file. Simply add these to your project and off you go.
40-
There is an example file - xmltest.cpp - to get you started.
38+
There is an example file - `xmltest.cpp` - to get you started.
4139

4240
TinyXML-2 is released under the ZLib license,
4341
so you can use it in open source or commercial code. The details
4442
of the license are at the top of every source file.
4543

4644
TinyXML-2 attempts to be a flexible parser, but with truly correct and
4745
compliant XML output. TinyXML-2 should compile on any reasonably C++
48-
compliant system. It does not rely on exceptions, RTTI, or the STL.
46+
compliant system. It does not rely on exceptions, run-time type information,
47+
or the C++ Standard Library.
4948

50-
What it doesn't do.
51-
-------------------
49+
## What it doesn't do.
5250

5351
TinyXML-2 doesn't parse or use DTDs (Document Type Definitions) or XSLs
5452
(eXtensible Stylesheet Language.) There are other parsers out there
5553
that are much more fully featured. But they are generally bigger and
5654
more difficult to use. If you are working with
5755
browsers or have more complete XML needs, TinyXML-2 is not the parser for you.
5856

59-
TinyXML-1 vs. TinyXML-2
60-
-----------------------
57+
## TinyXML-1 vs. TinyXML-2
6158

6259
TinyXML-2 has long been the focus of all development. It is well tested
6360
and should be used instead of TinyXML-1.
@@ -67,10 +64,10 @@ rich test cases. But the implementation of the parser is completely re-written
6764
to make it more appropriate for use in a game. It uses less memory, is faster,
6865
and uses far fewer memory allocations.
6966

70-
TinyXML-2 has no requirement or support for STL.
67+
TinyXML-2 has no dependency on the C++ Standard Library, and does not adapt or use any
68+
of its collection types either.
7169

72-
Features
73-
--------
70+
## Features
7471

7572
### Code Page
7673

@@ -81,17 +78,17 @@ Filenames for loading / saving are passed unchanged to the underlying OS.
8178

8279
### Memory Model
8380

84-
An XMLDocument is a C++ object like any other, that can be on the stack, or
85-
new'd and deleted on the heap.
81+
An `XMLDocument` is a C++ object like any other, that can be on the stack, or
82+
`new`'d and `delete`d on the heap.
8683

87-
However, any sub-node of the Document, XMLElement, XMLText, etc, can only
88-
be created by calling the appropriate XMLDocument::NewElement, NewText, etc.
84+
However, any sub-node of the `XMLDocument`, `XMLElement`, `XMLText`, etc, can only
85+
be created by calling the appropriate `XMLDocument::NewElement`, `XMLDocument::NewText`, etc.
8986
method. Although you have pointers to these objects, they are still owned
90-
by the Document. When the Document is deleted, so are all the nodes it contains.
87+
by the `XMLDocument`. When the `XMLDocument` is `delete`d, so are all the nodes it contains.
9188

9289
### White Space
9390

94-
#### Whitespace Preservation (default, PRESERVE_WHITESPACE)
91+
#### Whitespace Preservation (default, `PRESERVE_WHITESPACE`)
9592

9693
Microsoft has an excellent article on white space: http://msdn.microsoft.com/en-us/library/ms256097.aspx
9794

@@ -103,51 +100,57 @@ line-feed character, as required by the XML spec.
103100

104101
White space in text is preserved. For example:
105102

106-
<element> Hello, World</element>
103+
```xml
104+
<element> Hello, World</element>
105+
```
107106

108-
The leading space before the "Hello" and the double space after the comma are
107+
The leading space before the `"Hello"` and the double space after the comma are
109108
preserved. Line-feeds are preserved, as in this example:
110109

111-
<element> Hello again,
112-
World</element>
110+
```xml
111+
<element> Hello again,
112+
World</element>
113+
```
113114

114115
However, white space between elements is **not** preserved. Although not strictly
115116
compliant, tracking and reporting inter-element space is awkward, and not normally
116117
valuable. TinyXML-2 sees these as the same XML:
117118

118-
<document>
119-
<data>1</data>
120-
<data>2</data>
121-
<data>3</data>
122-
</document>
119+
```xml
120+
<document>
121+
<data>1</data>
122+
<data>2</data>
123+
<data>3</data>
124+
</document>
123125

124-
<document><data>1</data><data>2</data><data>3</data></document>
126+
<document><data>1</data><data>2</data><data>3</data></document>
127+
```
125128

126-
#### Whitespace Collapse (COLLAPSE_WHITESPACE)
129+
#### Whitespace Collapse (`COLLAPSE_WHITESPACE`)
127130

128131
For some applications, it is preferable to collapse whitespace. Collapsing
129132
whitespace gives you "HTML-like" behavior, which is sometimes more suitable
130133
for hand typed documents.
131134

132-
TinyXML-2 supports this with the 'whitespace' parameter to the XMLDocument constructor.
135+
TinyXML-2 supports this with the `whitespace` parameter to the `XMLDocument` constructor.
133136
(The default is to preserve whitespace, as described above.)
134137

135-
However, you may also use COLLAPSE_WHITESPACE, which will:
138+
However, you may also use `COLLAPSE_WHITESPACE`, which will:
136139

137140
* Remove leading and trailing whitespace
138141
* Convert newlines and line-feeds into a space character
139142
* Collapse a run of any number of space characters into a single space character
140143

141-
Note that (currently) there is a performance impact for using COLLAPSE_WHITESPACE.
144+
Note that (currently) there is a performance impact for using `COLLAPSE_WHITESPACE`.
142145
It essentially causes the XML to be parsed twice.
143146

144-
#### Pedantic Whitespace (PEDANTIC_WHITESPACE)
147+
#### Pedantic Whitespace (`PEDANTIC_WHITESPACE`)
145148

146149
For applications that need to know about text nodes that are composed entirely of
147-
whitespace, PEDANTIC_WHITESPACE is available. PEDANTIC_WHITESPACE maintains all the
150+
whitespace, `PEDANTIC_WHITESPACE` is available. `PEDANTIC_WHITESPACE` maintains all the
148151
whitespace between elements.
149152

150-
PEDANTIC_WHITESPACE is a new mode and not as tested as the other whitespace modes.
153+
`PEDANTIC_WHITESPACE` is a new mode and not as tested as the other whitespace modes.
151154

152155
### Error Reporting
153156

@@ -163,18 +166,22 @@ line number information for error messages.
163166
TinyXML-2 recognizes the pre-defined "character entities", meaning special
164167
characters. Namely:
165168

166-
&amp; &
167-
&lt; <
168-
&gt; >
169-
&quot; "
170-
&apos; '
169+
```html
170+
&amp; &
171+
&lt; <
172+
&gt; >
173+
&quot; "
174+
&apos; '
175+
```
171176

172177
These are recognized when the XML document is read, and translated to their
173178
UTF-8 equivalents. For instance, text with the XML of:
174179

175-
Far &amp; Away
180+
```xml
181+
Far &amp; Away
182+
```
176183

177-
will have the Value() of "Far & Away" when queried from the XMLText object,
184+
will have the `Value()` of `"Far & Away"` when queried from the `XMLText` object,
178185
and will be written back to the XML stream/file as an ampersand.
179186

180187
Additionally, any character can be specified by its Unicode code point:
@@ -188,114 +195,123 @@ regular code point. The output is correct, but the entity syntax isn't preserved
188195
#### Print to file
189196
You can directly use the convenience function:
190197

191-
XMLDocument doc;
192-
...
193-
doc.SaveFile( "foo.xml" );
198+
```cpp
199+
XMLDocument doc;
200+
// ...
201+
doc.SaveFile("foo.xml");
202+
```
194203

195-
Or the XMLPrinter class:
204+
Or the `XMLPrinter` class:
196205

197-
XMLPrinter printer( fp );
198-
doc.Print( &printer );
206+
```cpp
207+
XMLPrinter printer(fp);
208+
doc.Print(&printer);
209+
```
199210
200211
#### Print to memory
201-
Printing to memory is supported by the XMLPrinter.
212+
Printing to memory is supported by the `XMLPrinter`.
202213
203-
XMLPrinter printer;
204-
doc.Print( &printer );
205-
// printer.CStr() has a const char* to the XML
214+
```cpp
215+
XMLPrinter printer;
216+
doc.Print(&printer);
217+
// printer.CStr() has a const char* to the XML
218+
```
206219

207220
#### Print without an XMLDocument
208221

209222
When loading, an XML parser is very useful. However, sometimes
210223
when saving, it just gets in the way. The code is often set up
211224
for streaming, and constructing the DOM is just overhead.
212225

213-
The Printer supports the streaming case. The following code
226+
The `XMLPrinter` supports the streaming case. The following code
214227
prints out a trivially simple XML file without ever creating
215228
an XML document.
216229

217-
XMLPrinter printer( fp );
218-
printer.OpenElement( "foo" );
219-
printer.PushAttribute( "foo", "bar" );
220-
printer.CloseElement();
230+
```cpp
231+
XMLPrinter printer(fp);
232+
printer.OpenElement("foo");
233+
printer.PushAttribute("foo", "bar");
234+
printer.CloseElement();
235+
```
221236
222-
Examples
223-
--------
237+
### Examples
224238
225239
#### Load and parse an XML file.
226240
227-
/* ------ Example 1: Load and parse an XML file. ---- */
228-
{
229-
XMLDocument doc;
230-
doc.LoadFile( "dream.xml" );
231-
}
241+
```cpp
242+
/* ------ Example 1: Load and parse an XML file. ---- */
243+
{
244+
XMLDocument doc;
245+
doc.LoadFile("dream.xml");
246+
}
247+
```
232248

233249
#### Lookup information.
234250

235-
/* ------ Example 2: Lookup information. ---- */
236-
{
237-
XMLDocument doc;
238-
doc.LoadFile( "dream.xml" );
251+
```cpp
252+
/* ------ Example 2: Lookup information. ---- */
253+
{
254+
XMLDocument doc;
255+
doc.LoadFile("dream.xml");
239256

240-
// Structure of the XML file:
241-
// - Element "PLAY" the root Element, which is the
242-
// FirstChildElement of the Document
243-
// - - Element "TITLE" child of the root PLAY Element
244-
// - - - Text child of the TITLE Element
257+
// Structure of the XML file:
258+
// - Element "PLAY" the root Element, which is the
259+
// FirstChildElement of the Document
260+
// - - Element "TITLE" child of the root PLAY Element
261+
// - - - Text child of the TITLE Element
245262

246-
// Navigate to the title, using the convenience function,
247-
// with a dangerous lack of error checking.
248-
const char* title = doc.FirstChildElement( "PLAY" )->FirstChildElement( "TITLE" )->GetText();
249-
printf( "Name of play (1): %s\n", title );
263+
// Navigate to the title, using the convenience function,
264+
// with a dangerous lack of error checking.
265+
const char* title = doc.FirstChildElement("PLAY")->FirstChildElement("TITLE")->GetText();
266+
printf("Name of play (1): %s\n", title);
250267

251-
// Text is just another Node to TinyXML-2. The more
252-
// general way to get to the XMLText:
253-
XMLText* textNode = doc.FirstChildElement( "PLAY" )->FirstChildElement( "TITLE" )->FirstChild()->ToText();
254-
title = textNode->Value();
255-
printf( "Name of play (2): %s\n", title );
256-
}
268+
// Text is just another Node to TinyXML-2. The more
269+
// general way to get to the XMLText:
270+
XMLText* textNode = doc.FirstChildElement("PLAY")->FirstChildElement("TITLE")->FirstChild()->ToText();
271+
title = textNode->Value();
272+
printf("Name of play (2): %s\n", title);
273+
}
274+
```
257275

258-
Using and Installing
259-
--------------------
276+
## Using and Installing
260277

261278
There are 2 files in TinyXML-2:
262-
* tinyxml2.cpp
263-
* tinyxml2.h
279+
* `tinyxml2.cpp`
280+
* `tinyxml2.h`
264281

265282
And additionally a test file:
266-
* xmltest.cpp
283+
* `xmltest.cpp`
267284

268-
Generally speaking, the intent is that you simply include the tinyxml2.cpp and
269-
tinyxml2.h files in your project and build with your other source code.
285+
Generally speaking, the intent is that you simply include the `tinyxml2.cpp` and
286+
`tinyxml2.h` files in your project and build with your other source code.
270287

271288
There is also a CMake build included. CMake is the general build for TinyXML-2.
272289

273290
(Additional build systems are costly to maintain, and tend to become outdated. They are
274291
being removed over time.)
275292

276-
Building TinyXML-2 - Using vcpkg
277-
--------------------------------
293+
### Building TinyXML-2 - Using vcpkg
278294

279295
You can download and install TinyXML-2 using the [vcpkg](https://github.com/Microsoft/vcpkg) dependency manager:
280296

281-
git clone https://github.com/Microsoft/vcpkg.git
282-
cd vcpkg
283-
./bootstrap-vcpkg.sh
284-
./vcpkg integrate install
285-
./vcpkg install tinyxml2
297+
```sh
298+
git clone https://github.com/Microsoft/vcpkg.git
299+
cd vcpkg
300+
./bootstrap-vcpkg.sh
301+
./vcpkg integrate install
302+
./vcpkg install tinyxml2
303+
```
286304

287305
The TinyXML-2 port in vcpkg is kept up to date by Microsoft team members and community contributors. If the version is out of date, please [create an issue or pull request](https://github.com/Microsoft/vcpkg) on the vcpkg repository.
288306

289-
Versioning
290-
----------
307+
## Versioning
291308

292-
TinyXML-2 uses semantic versioning. http://semver.org/ Releases are now tagged in github.
309+
TinyXML-2 uses semantic versioning. http://semver.org/ Releases are now tagged in GitHub.
293310

294311
Note that the major version will (probably) change fairly rapidly. API changes are fairly
295312
common.
296313

297-
License
298-
-------
314+
## License
299315

300316
TinyXML-2 is released under the zlib license:
301317

@@ -316,8 +332,7 @@ must not be misrepresented as being the original software.
316332
3. This notice may not be removed or altered from any source
317333
distribution.
318334

319-
Contributors
320-
------------
335+
## Contributors
321336

322337
Thanks very much to everyone who sends suggestions, bugs, ideas, and
323338
encouragement. It all helps, and makes this project fun.

0 commit comments

Comments
 (0)