1//
2// PrettyPrint.cpp
3//
4// This sample demonstrates the SAXParser, WhitespaceFilter,
5// InputSource and XMLWriter classes.
6//
7// Copyright (c) 2004-2006, Applied Informatics Software Engineering GmbH.
8// and Contributors.
9//
10// SPDX-License-Identifier: BSL-1.0
11//
12
13
14#include "Poco/SAX/SAXParser.h"
15#include "Poco/SAX/WhitespaceFilter.h"
16#include "Poco/SAX/InputSource.h"
17#include "Poco/XML/XMLWriter.h"
18#include "Poco/Exception.h"
19#include <iostream>
20
21
22using Poco::XML::SAXParser;
23using Poco::XML::XMLReader;
24using Poco::XML::WhitespaceFilter;
25using Poco::XML::InputSource;
26using Poco::XML::XMLWriter;
27using Poco::Exception;
28
29
30int main(int argc, char** argv)
31{
32 // read XML from standard input and pretty-print it to standard output
33
34 SAXParser parser;
35 WhitespaceFilter filter(&parser);
36
37 XMLWriter writer(std::cout, XMLWriter::CANONICAL | XMLWriter::PRETTY_PRINT);
38 writer.setNewLine(XMLWriter::NEWLINE_LF);
39
40 filter.setContentHandler(&writer);
41 filter.setDTDHandler(&writer);
42 filter.setProperty(XMLReader::PROPERTY_LEXICAL_HANDLER, static_cast<Poco::XML::LexicalHandler*>(&writer));
43
44 try
45 {
46 InputSource source(std::cin);
47 filter.parse(&source);
48 }
49 catch (Exception& exc)
50 {
51 std::cerr << exc.displayText() << std::endl;
52 return 1;
53 }
54
55 return 0;
56}
57