1 /***************************************************************************
2 xmlcommon.cpp - Common XML functions
4 begin : Tue Apr 27 2004
5 copyright : (C) 2004 by Intra2net AG
6 email : info@intra2net.com
7 ***************************************************************************/
9 #include <xmlcommon.hpp>
10 #include <exception.hxx>
14 using namespace xmlpp;
19 void xml_validate_dtd(Node *root, const std::string &subsetname, const std::string &dtdname)
21 // Create empty document, set DTD
23 doc.set_internal_subset(subsetname, "", dtdname);
24 Element *newRoot = doc.create_root_node(root->get_name(), "", "");
27 Node::NodeList list = root->get_children();
28 Node::NodeList::iterator list_end = list.end();
29 for(xmlpp::Node::NodeList::iterator iter = list.begin(); iter != list_end; ++iter)
30 newRoot->import_node(*iter);
32 // Parse XML again to validate it
34 parser.set_validate();
35 parser.parse_memory(doc.write_to_string_formatted());
38 std::string xml_get_child_content(xmlpp::Node *base, const std::string &elementname)
40 vector<xmlpp::Node*> nodes;
43 nodes = base->find(elementname);
45 if (nodes.size() != 1)
48 os << "xml parse error at line " << base->get_line() << ": element ambiguous or not found: " << elementname;
49 throw EXCEPTION(runtime_error_src,os.str());
52 return xml_get_text_content(nodes[0], elementname);
55 std::string xml_get_text_content(xmlpp::Node *node, const std::string &elementname)
57 if (node->get_children().size() <= 0)
60 os << "xml parse error at line " << node->get_line() << ": element content not found in " << elementname;
61 throw EXCEPTION(runtime_error_src,os.str());
64 xmlpp::Node::NodeList childs = node->get_children();
65 xmlpp::Node::NodeList::iterator text = childs.begin();
66 const xmlpp::ContentNode *nodeText=dynamic_cast<const xmlpp::ContentNode*>(*text);
69 return nodeText->get_content();
73 os << "xml parse error at line " << (*text)->get_line() << ": element " << elementname << " does not contain content";
74 throw EXCEPTION(runtime_error_src,os.str());