Showing posts with label xml. Show all posts
Showing posts with label xml. Show all posts

Sunday, February 06, 2011

Attribute libxml2 xpath iphone howto

Attribute libxml2 xpath howto:

Given the xml:

<yournodewithanattribute someattribute="some attribute value"></yournodewithanattribute>

To get the attribute value:

NSString *path = [NSString stringWithFormat:@"http://yourdomain/yourxmlfile.xml"];
NSURL *url = [NSURL URLWithString:path];
NSData *xmlData = [NSData dataWithContentsOfURL:url];
NSString *xPathQuery1;
NSArray *nodes1;

xPathQuery1 = @"//yournodewithanattribute";

nodes1 = (NSArray*)PerformXMLXPathQuery(xmlData, xPathQuery1);

for (NSDictionary *attrNodes1 in nodes1){
for (NSDictionary *nodeAttributeArray in [attrNodes1 objectForKey:@"nodeAttributeArray"]){
NSString *attributeName = [nodeAttributeArray objectForKey:@"attributeName"];
NSString *attributeValue = [nodeAttributeArray objectForKey:@"nodeContent"];
NSLog(@"Attribute Name: %@", attributeName);
NSLog(@"Attribute Value: %@", attributeValue);
}
}

Output XML with PHP

Something to get you started with php and xml. Complete with get request and xml header:


<?php
header('Content-Type: text/xml');
echo '<?xml version="1.0" encoding="UTF-8" standalone="yes"?>';
?>
<?php
$getrequestparam1 = $_GET["getrequestparam1"];

echo "\n<result>No Results Found for $getrequestparam1</result>";
?>

Thursday, January 06, 2011

libxml2 ios example. libxml2 in xcode

This is a bit of a messy post, but it involves getting apps written using libxml2 to compile in Xcode.

1. Firstly, you'll need the libxml2 framework for Snow Leopard (and annoyingly, gcc to compile it).

You can download the latest libxml2 (get the .tar.gz) from here: ftp://xmlsoft.org/libxml2

Extract the files. Then do a

sudo ./configure
sudo make install

Boom. You're getting there. You can now open your Xcode environment.

2. When you first try to compile, you might get something like "libxml2 _xmlXPathNewContext referenced from" as an error.

This is because the library is not linked in the project yet.

You can link it by right clicking on the "Frameworks" folder, then "Add" -> "Existing Framework"

If libxml2 is installed on your system, you should be able to see it in the drop down box. As in the following screenshot:




3. The other thing that must be done. Is to put the string "/usr/include/libxml2/" in your search paths for your project. This is found by left clicking on your main project. Then clicking the blue 'info' button, followed by the 'build' tab.



I hope this helps someone save hours of frustration and searching like I did.