diff options
Diffstat (limited to 'docs/samples')
-rw-r--r-- | docs/samples/load_file.cpp | 14 | ||||
-rw-r--r-- | docs/samples/load_stream.cpp | 57 | ||||
-rw-r--r-- | docs/samples/tree.xml | 12 |
3 files changed, 67 insertions, 16 deletions
diff --git a/docs/samples/load_file.cpp b/docs/samples/load_file.cpp new file mode 100644 index 0000000..1a102c2 --- /dev/null +++ b/docs/samples/load_file.cpp @@ -0,0 +1,14 @@ +#include "pugixml.hpp"
+
+#include <iostream>
+
+int main()
+{
+//[code_load_file
+ pugi::xml_document doc;
+
+ pugi::xml_parse_result result = doc.load_file("tree.xml");
+
+ std::cout << "Load result: " << result.description() << ", mesh name: " << doc.child("mesh").attribute("name").value() << std::endl;
+//]
+}
diff --git a/docs/samples/load_stream.cpp b/docs/samples/load_stream.cpp index 4aba8bd..f97949d 100644 --- a/docs/samples/load_stream.cpp +++ b/docs/samples/load_stream.cpp @@ -14,6 +14,20 @@ void print_doc(const char* message, const pugi::xml_document& doc, const pugi::x << std::endl;
}
+bool try_imbue(std::wistream& stream, const char* name)
+{
+ try
+ {
+ stream.imbue(std::locale(name));
+
+ return true;
+ }
+ catch (const std::exception&)
+ {
+ return false;
+ }
+}
+
int main()
{
pugi::xml_document doc;
@@ -37,32 +51,43 @@ int main() {
// Since wide streams are treated as UTF-16/32 ones, you can't load the UTF-8 file from a wide stream
// directly if you have localized characters; you'll have to provide a UTF8 locale (there is no
- // standard one; you can use utf8_codecvt_facet from Boost (WHAT?))
+ // standard one; you can use utf8_codecvt_facet from Boost or codecvt_utf8 from C++0x)
std::wifstream stream("weekly-utf-8.xml");
- pugi::xml_parse_result result = doc.load(stream);
- // first character of root name: U+00E9, year: 1997
- print_doc("UTF8 file from wide stream", doc, result);
+ if (try_imbue(stream, "en_US.UTF-8")) // try Linux encoding
+ {
+ pugi::xml_parse_result result = doc.load(stream);
+
+ // first character of root name: U+00E9, year: 1997
+ print_doc("UTF8 file from wide stream", doc, result);
+ }
+ else
+ {
+ std::cout << "UTF-8 locale is not available\n";
+ }
}
{
- // Since wide streams are treated as UTF-16/32 ones, you can't load the UTF-16 file from a wide stream
- // at all; you'll have to provide a UTF16 locale (WHAT??)
- std::wifstream stream("weekly-utf-16.xml");
- pugi::xml_parse_result result = doc.load(stream);
-
- // first character of root name: U+0000, year:
- print_doc("UTF16 file from wide stream", doc, result);
+ // Since wide streams are treated as UTF-16/32 ones, you can't load the UTF-16 file from a wide stream without
+ // using custom codecvt; you can use codecvt_utf16 from C++0x
}
{
// Since encoding names are non-standard, you can't load the Shift-JIS (or any other non-ASCII) file
- // from a wide stream portably; this code assumes Microsoft C Runtime Libraries (WHAT???)
+ // from a wide stream portably
std::wifstream stream("weekly-shift_jis.xml");
- stream.imbue(std::locale(".932"));
- pugi::xml_parse_result result = doc.load(stream);
- // first character of root name: U+9031, year: 1997
- print_doc("Shift-JIS file from wide stream", doc, result);
+ if (try_imbue(stream, ".932") || // try Microsoft encoding
+ try_imbue(stream, "ja_JP.SJIS")) // try Linux encoding; run "localedef -i ja_JP -c -f SHIFT_JIS /usr/lib/locale/ja_JP.SJIS" to get it
+ {
+ pugi::xml_parse_result result = doc.load(stream);
+
+ // first character of root name: U+9031, year: 1997
+ print_doc("Shift-JIS file from wide stream", doc, result);
+ }
+ else
+ {
+ std::cout << "Shift-JIS locale is not available\n";
+ }
}
}
diff --git a/docs/samples/tree.xml b/docs/samples/tree.xml new file mode 100644 index 0000000..dbe3301 --- /dev/null +++ b/docs/samples/tree.xml @@ -0,0 +1,12 @@ +<?xml version="1.0"?>
+<mesh name="mesh_root">
+ <!-- here is a mesh node -->
+ some text
+ <![CDATA[[someothertext]]>
+ some more text
+ <node attr1="value1" attr2="value2" />
+ <node attr1="value2">
+ <innernode/>
+ </node>
+</mesh>
+<?include somedata?>
|