1 | // |
2 | // SAXParser.cpp |
3 | // |
4 | // This sample demonstrates the SAXParser class. |
5 | // |
6 | // Copyright (c) 2004-2006, Applied Informatics Software Engineering GmbH. |
7 | // and Contributors. |
8 | // |
9 | // SPDX-License-Identifier: BSL-1.0 |
10 | // |
11 | |
12 | |
13 | #include "Poco/SAX/SAXParser.h" |
14 | #include "Poco/SAX/ContentHandler.h" |
15 | #include "Poco/SAX/LexicalHandler.h" |
16 | #include "Poco/SAX/Attributes.h" |
17 | #include "Poco/SAX/Locator.h" |
18 | #include "Poco/Exception.h" |
19 | #include <iostream> |
20 | |
21 | |
22 | using Poco::XML::SAXParser; |
23 | using Poco::XML::XMLReader; |
24 | using Poco::XML::XMLString; |
25 | using Poco::XML::XMLChar; |
26 | using Poco::XML::ContentHandler; |
27 | using Poco::XML::LexicalHandler; |
28 | using Poco::XML::Attributes; |
29 | using Poco::XML::Locator; |
30 | |
31 | |
32 | class MyHandler: public ContentHandler, public LexicalHandler |
33 | { |
34 | public: |
35 | MyHandler(): |
36 | _pLocator(0) |
37 | { |
38 | } |
39 | |
40 | // ContentHandler |
41 | void setDocumentLocator(const Locator* loc) |
42 | { |
43 | _pLocator = loc; |
44 | } |
45 | |
46 | void startDocument() |
47 | { |
48 | where("startDocument" ); |
49 | } |
50 | |
51 | void endDocument() |
52 | { |
53 | where("endDocument" ); |
54 | } |
55 | |
56 | void startElement(const XMLString& uri, const XMLString& localName, const XMLString& qname, const Attributes& attributes) |
57 | { |
58 | where("startElement" ); |
59 | std::cout << "uri: " << uri << std::endl |
60 | << "localName: " << localName << std::endl |
61 | << "qname: " << qname << std::endl; |
62 | std::cout << "Attributes: " << std::endl; |
63 | for (int i = 0; i < attributes.getLength(); ++i) |
64 | { |
65 | std::cout << attributes.getLocalName(i) << "=" << attributes.getValue(i) << std::endl; |
66 | } |
67 | } |
68 | |
69 | void endElement(const XMLString& uri, const XMLString& localName, const XMLString& qname) |
70 | { |
71 | where("endElement" ); |
72 | } |
73 | |
74 | void characters(const XMLChar ch[], int start, int length) |
75 | { |
76 | where("characters" ); |
77 | std::cout << std::string(ch + start, length) << std::endl; |
78 | } |
79 | |
80 | void ignorableWhitespace(const XMLChar ch[], int start, int length) |
81 | { |
82 | where("ignorableWhitespace" ); |
83 | } |
84 | |
85 | void processingInstruction(const XMLString& target, const XMLString& data) |
86 | { |
87 | where("processingInstruction" ); |
88 | std::cout << "target=" << target << ", data=" << data << std::endl; |
89 | } |
90 | |
91 | void startPrefixMapping(const XMLString& prefix, const XMLString& uri) |
92 | { |
93 | where("startPrefixMapping" ); |
94 | std::cout << "prefix=" << prefix << " uri=" << uri << std::endl; |
95 | } |
96 | |
97 | void endPrefixMapping(const XMLString& prefix) |
98 | { |
99 | where("endPrefixMapping" ); |
100 | std::cout << "prefix=" << prefix << std::endl; |
101 | } |
102 | |
103 | void skippedEntity(const XMLString& name) |
104 | { |
105 | where("skippedEntity" ); |
106 | std::cout << "name=" << name << std::endl; |
107 | } |
108 | |
109 | // LexicalHandler |
110 | void startDTD(const XMLString& name, const XMLString& publicId, const XMLString& systemId) |
111 | { |
112 | where("startDTD" ); |
113 | } |
114 | |
115 | void endDTD() |
116 | { |
117 | where("endDTD" ); |
118 | } |
119 | |
120 | void startEntity(const XMLString& name) |
121 | { |
122 | where("startEntity" ); |
123 | } |
124 | |
125 | void endEntity(const XMLString& name) |
126 | { |
127 | where("endEntity" ); |
128 | } |
129 | |
130 | void startCDATA() |
131 | { |
132 | where("startCDATA" ); |
133 | } |
134 | |
135 | void endCDATA() |
136 | { |
137 | where("endCDATA" ); |
138 | } |
139 | |
140 | void comment(const XMLChar ch[], int start, int length) |
141 | { |
142 | where("comment" ); |
143 | } |
144 | |
145 | protected: |
146 | void where(const std::string& meth) |
147 | { |
148 | std::cout << "*** " << meth; |
149 | if (_pLocator) |
150 | { |
151 | std::cout << " in " |
152 | << _pLocator->getSystemId() |
153 | << ", line " << _pLocator->getLineNumber() |
154 | << ", col " << _pLocator->getColumnNumber() |
155 | << std::endl; |
156 | } |
157 | } |
158 | |
159 | private: |
160 | const Locator* _pLocator; |
161 | }; |
162 | |
163 | |
164 | int main(int argc, char** argv) |
165 | { |
166 | // parse an XML document and print the generated SAX events |
167 | |
168 | if (argc < 2) |
169 | { |
170 | std::cout << "usage: " << argv[0] << ": <xmlfile>" << std::endl; |
171 | return 1; |
172 | } |
173 | |
174 | MyHandler handler; |
175 | |
176 | SAXParser parser; |
177 | parser.setFeature(XMLReader::FEATURE_NAMESPACES, true); |
178 | parser.setFeature(XMLReader::FEATURE_NAMESPACE_PREFIXES, true); |
179 | parser.setContentHandler(&handler); |
180 | parser.setProperty(XMLReader::PROPERTY_LEXICAL_HANDLER, static_cast<LexicalHandler*>(&handler)); |
181 | |
182 | try |
183 | { |
184 | parser.parse(argv[1]); |
185 | } |
186 | catch (Poco::Exception& e) |
187 | { |
188 | std::cerr << e.displayText() << std::endl; |
189 | return 2; |
190 | } |
191 | |
192 | return 0; |
193 | } |
194 | |