Overview
Features
Download
Documentation
Community
Add-Ons & Services

Parsing XML from URI

Please post support and help requests here.

Parsing XML from URI

Postby martin678 » 20 Nov 2012, 06:08

I just started using POCO on the QNX platform.
I'm having a hard time parsing an XML document from the web.
How do I convert the istream provided by HTTPClientSession::receiveResponse() to something suitable as an InputSource for the DOMParser?
Thank you
martin678
 
Posts: 5
Joined: 20 Nov 2012, 05:17

Re: Parsing XML from URI

Postby guenter » 20 Nov 2012, 09:24

Here's an example for how to parse an XML document with the DOM parser from an URI.
Generally, if you have an std::istream, you'll have to create a Poco::XML::InputSource from it and pass the InputSource to the DOMParser.

Code: Select all
#include "Poco/DOM/DOMParser.h"
#include "Poco/DOM/Document.h"
#include "Poco/DOM/NodeIterator.h"
#include "Poco/DOM/NodeFilter.h"
#include "Poco/DOM/AutoPtr.h"
#include "Poco/SAX/InputSource.h"
#include "Poco/SAX/XMLReader.h"
#include "Poco/Net/HTTPStreamFactory.h"
#include "Poco/Net/FTPStreamFactory.h"
#include "Poco/URIStreamOpener.h"
#include "Poco/SharedPtr.h"
#include "Poco/Exception.h"
#include <iostream>


using Poco::XML::DOMParser;
using Poco::XML::InputSource;
using Poco::XML::Document;
using Poco::XML::NodeIterator;
using Poco::XML::NodeFilter;
using Poco::XML::Node;
using Poco::XML::XMLReader;
using Poco::XML::AutoPtr;
using Poco::Exception;


int main(int argc, char** argv)
{
   Poco::Net::HTTPStreamFactory::registerFactory();
   Poco::Net::FTPStreamFactory::registerFactory();
   try
   {
      Poco::SharedPtr<std::istream> pStream = Poco::URIStreamOpener::defaultOpener().open("http://pocoproject.org/blog/?feed=rss2");
      InputSource src(*pStream);

      DOMParser parser;      
      parser.setFeature(XMLReader::FEATURE_NAMESPACES, true);
      parser.setFeature(XMLReader::FEATURE_NAMESPACE_PREFIXES, true);

      AutoPtr<Document> pDoc = parser.parse(&src);
      
      // Now use node iterator to print out all nodes
      NodeIterator it(pDoc, NodeFilter::SHOW_ALL);
      Node* pNode = pNode = it.nextNode();
      while (pNode)
      {
         std::cout << pNode->nodeName() << ":" << pNode->nodeValue() << std::endl;
         pNode = it.nextNode();
      }
   }
   catch (Exception& exc)
   {
      std::cerr << exc.displayText() << std::endl;
   }
   
   return 0;
}
guenter
 
Posts: 1135
Joined: 11 Jul 2006, 16:27
Location: Austria

Re: Parsing XML from URI

Postby martin678 » 20 Nov 2012, 16:09

guenter,
Thank you for the prompt response. I did my homework again to discover I had forgotten to use libPocoXML, the reason why the linker refused my InputSource creation line (undefined reference...).

However, the code you provided won't build in my dev environment. It throws:
In function `Poco::AtomicCounter::operator--()':
N:/Embedded/Projets/contrib/poco/1.4.4/include/Poco/AtomicCounter.h:275: undefined reference to `__sync_sub_and_fetch_4'
I got the same error when playing around with Logger samples from the PDF documentation.
Here's a part of the common.mk file that shows include paths, lib paths and libs for the code you provided:
Code: Select all
EXTRA_INCVPATH+=N:/Embedded/Projets/contrib/poco/1.4.4/include
EXTRA_LIBVPATH+=N:/Embedded/Projets/contrib/poco/1.4.4/lib
LIBS+=PocoFoundation PocoXML PocoNet socket m


Any idea what could be missing?
I build for QNX Neutrino 6.4.1 in a cross-platform environment (Windows 7 32-bit host).
My system shows it's using gcc 4.3.3: 'C:\QNX641\host\win32\x86\etc\qcc\gcc\4.3.3'
I built the Poco libs natively on QNX 641 using gcc 4.3.3 without a single warning.

Thank you again
Martin
martin678
 
Posts: 5
Joined: 20 Nov 2012, 05:17

Re: Parsing XML from URI

Postby guenter » 20 Nov 2012, 16:19

It seems that your toolchain does not support GCC atomics. Re-build POCO with -DPOCO_NO_GCC_ATOMICS. You can either put it into the build/config/QNX file, or ./configure --cflags=-DPOCO_NO_GCC_ATOMICS. However, iterestingely, when I tested the build on QNX 6.4.1 it worked out of the box.
guenter
 
Posts: 1135
Joined: 11 Jul 2006, 16:27
Location: Austria

Re: Parsing XML from URI

Postby martin678 » 20 Nov 2012, 17:39

I didn't test the build though. I will test my current build and post the results here.
I'll also build and test with -DPOCO_NO_GCC_ATOMICS.
Thank you
martin678
 
Posts: 5
Joined: 20 Nov 2012, 05:17

Re: Parsing XML from URI

Postby martin678 » 05 Dec 2012, 20:11

guenter wrote:Re-build POCO with -DPOCO_NO_GCC_ATOMICS. You can either put it into the build/config/QNX file, or ./configure --cflags=-DPOCO_NO_GCC_ATOMICS.


I re-built POCO without GCC ATOMICS (./configure way) and I still get the error:
In function `Poco::AtomicCounter::operator--()':
/usr/local/include/Poco/AtomicCounter.h:275: undefined reference to `__sync_sub_and_fetch_4'

Any idea how to work around this?
Thank you
martin678
 
Posts: 5
Joined: 20 Nov 2012, 05:17

Re: Parsing XML from URI

Postby alex » 05 Dec 2012, 20:59

martin678 wrote:Any idea how to work around this?

Did you clean and rebuild? The 1.4.4 code below shows the preprocessor logic in AtomicCounter.h; look at it and see what's breaking it on your platform. If you are in a hurry, just comment the define but it would be nice if you could submit back the proper patch.

Code: Select all
#if POCO_OS == POCO_OS_WINDOWS_NT
#include "Poco/UnWindows.h"
#elif POCO_OS == POCO_OS_MAC_OS_X
#include <libkern/OSAtomic.h>
#elif ((__GNUC__ == 4) && (__GNUC_MINOR__ >= 2) || __GNUC__ > 4) && (defined(__x86_64__) || defined(__i386__))
#if !defined(POCO_HAVE_GCC_ATOMICS) && !defined(POCO_NO_GCC_ATOMICS)
#define POCO_HAVE_GCC_ATOMICS
#endif
#elif ((__GNUC__ == 4) && (__GNUC_MINOR__ >= 3) || __GNUC__ > 4)
#if !defined(POCO_HAVE_GCC_ATOMICS) && !defined(POCO_NO_GCC_ATOMICS)
#define POCO_HAVE_GCC_ATOMICS
#endif
#endif // POCO_OS
alex
 
Posts: 1130
Joined: 11 Jul 2006, 16:27
Location: United_States

Re: Parsing XML from URI

Postby martin678 » 05 Dec 2012, 21:59

Rebuilding from a fresh source tree produced libs that were the exact same size as the ones built without the option. Normal?
Looking at the build output I noticed the samples were built using the -DPOCO_NO_GCC_ATOMICS option. Adding it to my own projects made it: they now build on my original POCO 1.4.4 libraries (built without the POCO_NO_GCC_ATOMICS option).

I'm rushing a bit to deliver projects before XMas but I still can take some time to test builds if you wish.
Thanks!
martin678
 
Posts: 5
Joined: 20 Nov 2012, 05:17


Return to Support

Who is online

Users browsing this forum: No registered users and 1 guest

cron