void validateXml(const string& sXML, const string& sSchema, const string& sXMLName, const string& sSchemaName) { XMLParser parser; parser.setSchema(sSchema, sSchemaName); parser.parse(sXML, sXMLName); }
ParserElement::ref StringTreeParser::parse(const std::string &xml) { PlatformXMLParserFactory factory; DefaultStringTreeParser client; XMLParser *parser = factory.createXMLParser(&client); parser->parse(xml); ParserElement::ref root = client.getRoot(); delete parser; return root; }
ParserElement::ref StringTreeParser::parse(const std::string &xml) { PlatformXMLParserFactory factory; DefaultStringTreeParser client; #if (SWIFTEN_VERSION >= 0x040000) std::unique_ptr<XMLParser> parser = factory.createXMLParser(&client); #else XMLParser *parser = factory.createXMLParser(&client); #endif parser->parse(xml); ParserElement::ref root = client.getRoot(); #if (SWIFTEN_VERSION < 0x040000) delete parser; #endif return root; }
void createElementTest() { std::basic_string<utf16_t> source = Transcoder::UTF8toUTF16("<?xml\n version=\"1.0\" \n" "standalone=\"yes\"?>" "<test><hoge /><!-- comment -->" "<fuga><![CDATA[<hoge><fuga>" "]]><fuga attr=\"atr\"/>test</fuga></test> "); XMLParser<utf16_t> parser; XMLDocument<utf16_t>* document = parser.parse(source); XMLPath<utf16_t> path(Transcoder::UTF8toUTF16("/test/fuga/#text")); std::vector<Element<utf16_t>*> elements = path.evaluate(document); CPPUNIT_ASSERT(elements.size() == 1); CPPUNIT_ASSERT_MESSAGE( Transcoder::UTF16toUTF8( dynamic_cast<const CDATAElement<utf16_t>*>( elements[0])->getString()), Transcoder::UTF16toUTF8( dynamic_cast<const CDATAElement<utf16_t>*>( elements[0])->getString()) == "<hoge><fuga>"); delete document; }
int main(const int argc, const char** argv) { const char * file[]{ "./Data/10.pep.xml", "/home/matthijs/Dropbox/Studie/Hogeschool Leiden/Stage/Data/10.pep.xml", "/users/Matthijs/Dropbox/Studie/Hogeschool Leiden/Stage/Data/10.pep.xml", "/home/matthijs/Dropbox/Studie/Hogeschool Leiden/Stage/Data/Adult_Lung_Gel_Velos_13_f06.mzXML", "C:\\Users\\Matthijs Kuik\\Dropbox\\Studie\\Hogeschool Leiden\\Stage\\Data\\10.pep.xml", "/media/matthijs/Windows/Documents and Settings/Matthijs/Dropbox/Studie/Hogeschool Leiden/Stage/Data/10.pep.xml", "/storage/emulated/0/CppDroid/projects/xpxml/Data/10.pep.xml"}; const char * IN = NULL; const char * PATH = NULL; // 3,4:error const char * OUT = NULL; XMLParser::Method METHOD = XMLParser::GET_VALUES; unsigned int *limit = 0; for(int i = 0; i != argc; i++) { if(strcmp(argv[i], "-xml") == 0) { METHOD = XMLParser::TO_XML; } else if(i + 1 != argc) { if(strcmp(argv[i], "-i") == 0) { IN = argv[i + 1]; } else if(strcmp(argv[i], "-o") == 0) { OUT = argv[i + 1]; } else if(strcmp(argv[i], "-x") == 0) { PATH = argv[i + 1]; } else if(strcmp(argv[i], "-max") == 0) { limit = new unsigned int( convert<unsigned int>(argv[i + 1])); } else if(strcmp(argv[i], "-t") == 0) { try { PATH = param[convert<int>(argv[i + 1])]; } catch(std::exception &) { std::printf("error: expected integer after -t option"); showUsageWarning(argv[0]); } } else if(strcmp(argv[i], "-h") == 0) { showUsageWarning(argv[0]); } } } std::printf("xpath: %s\n", PATH); if(IN == NULL) showUsageWarning(argv[0]); if(PATH == NULL) showUsageWarning(argv[0]); Globals::FILESIZE = fileSize(IN); ofstream of; try { XMLParser * parser = 0; if(OUT == NULL) { parser = new XMLParser(std::cout); } else { of.open(OUT, std::ios::out); parser = new XMLParser(of); } if(limit) { parser->setNodeIDLimit(*limit); delete limit; } parser->parse(IN, PATH, METHOD); delete parser; } catch(std::runtime_error& e) { std::cout << e.what() << "\n"; } of.close(); //char c; // std::cin >> c; return 0; }
void QtgdataClient::onAtomFeedRetrieved(QByteArray reply) { #ifdef QTGDATA_DEBUG qDebug() << "onAtomFeedRetrieved. Client ID: " << ID; #endif XMLParser parser; try { IEntity *entity = parser.parse(reply,reply.size()); if((entity != NULL)&&(entity->getId() != Id::NULLID)) { if(entity->getId() == Id::feed) { atomFeed = createAtomFeed(); IEntity::itConstEntities begin,end; entity->getEntityList(begin,end); if(begin != end) { for(IEntity::itConstEntities it = begin; it != end; it++ ) { IEntity *sit = dynamic_cast<IEntity *>(*it); switch(sit->getId()) { case Id::id: atomFeed->id = sit->getValue(); break; case Id::updated: atomFeed->updated.fromString(sit->getValue(),Qt::ISODate); break; case Id::title: atomFeed->title = sit->getValue(); break; case Id::generator: atomFeed->generator.generator = sit->getValue(); atomFeed->generator.version = sit->getAttribute(AttributeId::version)->sValue; atomFeed->generator.uri = QUrl(sit->getAttribute(AttributeId::uri)->sValue); break; case Id::author: { IEntity *aux = NULL; Author author; if((aux=sit->getEntity(Id::name))) author.name = aux->getValue(); aux = NULL; if(aux=sit->getEntity(Id::uri)) author.uri = QUrl(aux->getValue()); aux = NULL; if(aux=sit->getEntity(Id::email)) author.email = aux->getValue(); atomFeed->authors.append(author); break; } case Id::link: { Link link; link.href = QUrl(sit->getAttribute(AttributeId::href)->sValue); link.rel = sit->getAttribute(AttributeId::rel)->sValue; link.type = sit->getAttribute(AttributeId::type)->sValue; atomFeed->links.append(link); break; } case Id::entry: { AtomEntry *atomEntry = createAtomEntry(); IEntity::itConstEntities entryBegin,entryEnd; sit->getEntityList(entryBegin,entryEnd); for(IEntity::itConstEntities itEntry = entryBegin; itEntry != entryEnd; itEntry++) { IEntity *entry = dynamic_cast<IEntity *>(*itEntry); int id = entry->getId(); switch(id) { case Id::author: { IEntity *author = entry->getEntity(Id::author); if((author)&&(author->getId())) { IEntity *aux = NULL; if((aux=author->getEntity(Id::name))) { Author author; author.name = aux->getValue(); atomEntry->authors.append(author); } } break; } case Id::id: atomEntry->id = entry->getValue(); break; case Id::published: atomEntry->published.fromString(entry->getValue(),Qt::ISODate); break; case Id::updated: atomEntry->updated.fromString(entry->getValue(),Qt::ISODate); break; case Id::summary: atomEntry->summary = entry->getValue(); break; case Id::title: atomEntry->title = entry->getValue(); break; case Id::link: { Link link; link.href = QUrl(entry->getAttribute(AttributeId::href)->sValue); link.rel = entry->getAttribute(AttributeId::rel)->sValue; link.type = entry->getAttribute(AttributeId::type)->sValue; atomEntry->links.append(link); break; } case Id::content: { atomEntry->content.type = entry->getAttribute(AttributeId::type)->sValue; atomEntry->content.content = QByteArray(entry->getValue().toAscii());; break; } default: parseEntry(id,atomEntry,entry); break; } } appendEntry(atomEntry); } } } } emitAtomFeedRetrieved(); } // if feed } delete entity; } catch(XMLParserException e) { qDebug() << e.what(); } }
document* XMLFile::load(const iconvplus::MemoryBlock& Dump) { if (Dump.first > 3) throw XMLError(XMLError::FILE_TOO_SMALL); std::string encoding; uint32_t BOM = *reinterpret_cast<const uint32_t*>(Dump.second.get()); if ((BOM & 0x00ffffff) == 0xbfbbef) { encoding = "UTF-8"; } else if (((BOM & 0x0000ffff) == 0xfffe) || (BOM == 0x003f003c)) { encoding = "UTF-16LE"; } else if (((BOM & 0x0000ffff) == 0xfeff) || (BOM == 0x3f003c00)) { encoding = "UTF-16BE"; } if (encoding.empty()) { if ((BOM & 0x0000ffff) == 0x3f3c)//8bit or UTF8 { size_t end = Dump.first; uint8_t *content = Dump.second.get(); char *gt = reinterpret_cast<char*>(memchr(content, '>', end)); if (gt) { *gt = '\0'; #ifdef _WIN32 char *encoding_start = strstr(reinterpret_cast<char*>(content), "encoding"); #else char *encoding_start = strcasestr(reinterpret_cast<char*>(content), "encoding"); #endif if (encoding_start) { size_t quot_open = strcspn(encoding_start, "\"'"); char quot_char = encoding_start[quot_open]; if (quot_char) { char *quot_close = strchr(encoding_start + quot_open + 1, quot_char); if (quot_close) encoding.assign(encoding_start + quot_open + 1, quot_close - (encoding_start + quot_open + 1)); } } *gt = '>'; } } } if (encoding.empty()) encoding = DEFAULT_ENCODING; iconvplus::MemoryBlock eDumpIn = Dump.copy(); iconvplus::MemoryBlock eDumpOut; if (iconvplus::convert(eDumpIn, eDumpOut, encoding, "WCHAR_T")) throw XMLError(XMLError::FILE_UNKNOWN_ENCODING); XMLParser parser; document *XMLDoc = parser.parse(eDumpOut); if (parser.getErrorCode()) { delete XMLDoc; throw XMLError(parser.getErrorCode(), parser.getPos(), parser.getLine(), parser.getChar()); } return XMLDoc; }