IPB SourceForge.net Logo

Welcome Guest ( Log In | Register )

[ Outline ] · Standard · Linear+

> XML Kit

ddiego
post Oct 24 2007, 08:00 PM
Post #1


VCF Lead Developer
***

Group: Lead Developer
Posts: 2834
Joined: 2-August 03
Member No.: 2



Yes we do support basic XML in the FoundationKit, but it's very basic.

So, if you need full blown XML support, and things like xpath, then the XML kit is your lib smile.gif

I have wrapped the libXML c library and we now support a SAX2 parser, a TextReader based xml parser, full node/document iteration, and support for XPath.

All of this just puts a very thin, but hopefully easy to use, wrapper over libXML, so most of the functionality found in that library is made available here. Ultimately XSLT support could be added as well, but I'll hold off unless feels a burning desire to have that.

As always, any thoughts on this would be most welcome!

Peruse the XMLKit code via SVN

Some samples of the code:

SAX parser handler
CODE


void MystartElementSAXFunc2( const xmlChar * name,
     const xmlChar ** atts)
{
printf( "@MystartElementSAXFunc2 name: %s\n", (const char*) name );

if ( NULL != atts ) {
 const xmlChar** tmp = atts;
 while ( *tmp != 0 ) {
  printf( "attr: %s\n", (const char*)*tmp );
  tmp ++;
 }
}
}



XMLSaxParser p;
p.StartElement += MystartElementSAXFunc2;
p.parse( testxml );




TextReader usage:
CODE

XMLTextReader rdr;
rdr.setXML( testxml );
while ( rdr.read() ) {  
System::println( "Name: " + rdr.getName() + " depth: " + rdr.getCurrentDepth() );

System::println( "Inner XML: { " + rdr.readInnerXml() + " }" );
}


Working with xml nodes directly, using async URLs to download the data:
CODE

AsyncURL url("http://www.w3schools.com/xpath/books.xml");
url.get();
url.wait();

String xml = url.getDataAsString();
XmlDocument doc;
doc.setXML(xml);

XmlNode root = doc.getRoot();
std::vector<XmlNode> children;
root.getChildren( children );
std::vector<XmlNode>::iterator cit = children.begin();
while ( cit != children.end() ) {
XmlNode& child = *cit;
System::println( "Name: " + child.getName() + ", path: " + child.getPath() );
++cit;
}



Working with XPath:
CODE

AsyncURL url("http://www.w3schools.com/xpath/books.xml");
url.get();
url.wait();
String xml = url.getDataAsString();

XmlDocument doc;
doc.setXML(xml);

std::vector<XmlNode> nodes;
VariantData res = doc.matches( s, nodes );

System::println( "doc.matches() returned: " + res.toString() );

for (size_t i=0;i<nodes.size();i++ ) {
const XmlNode& n = nodes[i];  
System::println( "Node name: " + n.getName() + " path: " + n.getPath() + " text: " + n.getContent() );
}


XPath using the xpath iterator:
CODE

XmlDocument doc;
doc.setXML(xml);

XPathIterator xp(doc);
XPathExpression exp;
exp.compile( "/bookstore/book" );

XPathNodeIterator it = xp.selectNodes(exp);
while ( it != xp.end() ) {
const XmlNode& n = *it;

System::println( "Node: " + n.getPath() );
it ++;
}

it = xp.begin();
do {
const XmlNode& n = *it;
System::println( "Node: " + n.getName() + " contents:\n" + n.getContent() );
} while ( it.next() );


xpath with namespace support:
CODE

XmlDocument doc;
doc.setXML( BooksXML );
 
XPathIterator xp(doc);
Dictionary nsDict;
nsDict["bk"] = "http://www.example.com/books";
xp.registerNamespaces(nsDict);
 
XPathNodeIterator it = xp.selectNodes("/books/bk:book");
while ( it != xp.end() && !it.isNull() ) {
const XmlNode& n = *it;

std::vector<XmlNamespace> ns;
n.getNamespaceList(ns);

std::vector<XmlNamespace>::iterator itn = ns.begin();
while ( itn != ns.end() ) {
 XmlNamespace& xns = *itn;
 System::println( "Node ns info - prefix: " + xns.getPrefix() + " uri: " + xns.getURI() );
 ++itn;
}


System::println( "Node: " + n.getPath() );    
it ++;
}




Just an update:

creating an xml doc from scratch:
CODE

XmlDocument* doc = XmlDocument::newDocument();

root = doc->newNode( "n1" );

doc2->setRoot( root );
 
root.addChild( doc->newNode( "spaz" ) );
root.addChild( doc->newNode( "goofy" ) );
root.addChild( doc->newNode( "donald" ) );

XPathIterator xp(*doc);
xp.select("/n1/goofy").setContent( "Hello World" );

root.setAttribute( "happy", "yes" );
xp.select("/n1/spaz").setAttributeAsVariant( "noise", 10 );

FileOutputStream fos("doc.xml");
TextOutputStream tos(&fos);
tos << doc->toString();

delete doc;



the final file looks like this:
CODE

<?xml version="1.0"?>
<n1 happy="yes">
 <spaz noise="10"></spaz>
 <goofy>Hello World</goofy>
 <donald></donald>
</n1>
User is offlineProfile CardPM
Go to the top of the page
+Quote Post

Posts in this topic
ddiego   XML Kit   Oct 24 2007, 08:00 PM


Reply to this topicTopic OptionsStart new topic
1 User(s) are reading this topic (1 Guests and 0 Anonymous Users)
0 Members:
 

Lo-fi Version : 18th May 2013 - 07:13 PM