iPhone NSXMLParse for .asx files - iphone

I'm totally new to XML.
Does anyone have a sample code to help me build a custom class that would parse Microsoft .asx files to play mms streams in sequence on the iPhone?
Some Googling revealed to me that .xml and .asx are somewhat related, though the second one is very limited if compared to the first.
I need to play three streams in sequence, inside an .asx container like this:
<asx version="3.0">
<TITLE>MYSONGS</TITLE>
<entry>
<TITLE>www.mysite.com</TITLE>
<ref href="mms://mysite.com/musicFolder/song1.wma" />
</entry>
<entry>
<TITLE>MYSONGS</TITLE>
<ref href="mms://mysite.com/musicFolder/song2.wma" />
</entry>
<entry>
<TITLE>www.mysite.com</TITLE>
<ref href="mms://mysite.com/musicFolder/song3.wma" />
</entry>
</asx>
I'm already able to parse the mms stream, decode and play the wma file. I'm just not able yet to parse .asx content, to play the streams in sequence. Thanks!

In my case I've been able to successfully parse .asx files using TouchXML, using this code (that I modified from the original version available at http://foobarpig.com/iphone/parsing-xml-element-attributes-with-touchxml.html):
// we will put parsed data in an a array
NSMutableArray *res = [[NSMutableArray alloc] init];
NSURL *url = [NSURL URLWithString: #"http://www.yoursite.com/WindowsMediaDotCom/theFileToParse.asx"];
/* have TouchXML parse it into a CXMLDocument */
CXMLDocument *document = [[[CXMLDocument alloc] initWithContentsOfURL:url options:0 error:nil] autorelease];
NSArray *nodes = NULL;
// searching for piglet nodes
nodes = [document nodesForXPath:#"//ref" error:nil];
for (CXMLElement *node in nodes) {
NSMutableDictionary *item = [[NSMutableDictionary alloc] init];
int counter;
for(counter = 0; counter < [node childCount]; counter++) {
// common procedure: dictionary with keys/values from XML node
[item setObject:[[node childAtIndex:counter] stringValue] forKey:[[node childAtIndex:counter] name]];
}
// and here it is - attributeForName! Simple as that.
[item setObject:[[node attributeForName:#"href"] stringValue] forKey:#"href"]; // <------ this magical arrow is pointing to the area of interest
[res addObject:item];
[item release];
}
// and we print our results
NSLog(#"%#", res);

Related

XCODE NSXML changing the element value

I have a very simple xml file by name options.xml
<Dat>
<Name>Tom</Name>
<Option>1</Option>
</Dat>
Using NSXML I am trying to change "Tom" to "Jim" and save the file. How can I do that. I read many document and there is no straight forward solution. Can some one help me with the code ?
update: I ended up in trying with Gdatasxml
-(void)saveToXML
{
NSString* path = [[NSBundle mainBundle] pathForResource:#"options" ofType:#"xml"];
NSData *xmlData = [[NSMutableData alloc] initWithContentsOfFile:path];
NSError *error;
GDataXMLDocument *doc = [[GDataXMLDocument alloc] initWithData:xmlData options:0 error:&error];
GDataXMLElement *rootElement = [GDataXMLElement elementWithName:#"Dat"];
NSArray *mySettings = [doc.rootElement elementsForName:#"Dat"];
for (GDataXMLElement *mySet in mySettings)
{
NSString *name;
NSArray *names = [mySet elementsForName:#"Name"];
if (names.count > 0)
{
GDataXMLElement *childElement = (GDataXMLElement *) [names objectAtIndex:0];
name = childElement.stringValue;
NSLog(childElement.stringValue);
[childElement setStringValue:#"Jim"];
}
}
[xmlData writeToFile:path atomically:YES];
}
But this is not saving the data. Help.
Editing XML is a little difficult in iOS. You need to parse the original xml to a model and then form the xml.
You can make use of 3rd party library such as GDataXML for forming XML from a data source.
//Edited user info saved in a dictionary
NSDictionary *dictionary = #{#"Name": #"Jim", #"Option":#"1"};
GDataXMLElement *rootElement = [GDataXMLElement elementWithName:#"Dat"];
[dictionary enumerateKeysAndObjectsUsingBlock:^(id key, id obj, BOOL *stop) {
GDataXMLElement *element = [GDataXMLElement elementWithName:key stringValue:obj];
[rootElement addChild:element];
}];
//xml document is formed
GDataXMLDocument *document = [[GDataXMLDocument alloc]
initWithRootElement:rootElement];
NSData *xmlData = document.XMLData;
NSString *filePath = [self savedXMLPath];
//XML Data is written back to a filePath
[xmlData writeToFile:filePath atomically:YES];
Create a class that is essentially an XML "node". Then in your parser setup a system of these XML nodes in the same fashion as you read them. Then search through that body and find the element that you would like to change. Change it. Then write a function that goes through these "node" objects and writes a new NSString in XML format and save that string to file. There is no real easy way that I know of to write XML files. I'm sure someone has a library out there to do it, but I had very complex XML's to deal with so I wrote my own. If you would like specific code let me know and I can try to give you parts of what you may need.
You Can use GDATAXML for changing XML node
Here is Working Code snippet
NSString *XMLString = #"<Dat><Name>Tom</Name><Option>1</Option></Dat>";
NSError *error = nil;
GDataXMLElement *newElement = [[GDataXMLElement alloc] initWithXMLString: XMLString error: &error];
NSLog(#"New element: %# error: %#", newElement, error);
if(nil == error)
{
GDataXMLElement *childElement = [[newElement elementsForName: #"Name"] objectAtIndex: 0];
[childElement setStringValue:#"Jim"];
childElement = [[newElement elementsForName: #"Option"] objectAtIndex: 0];
[childElement setStringValue:#"2"];
}
NSLog(#"New element now: %#", newElement);
Check by using this code snippet

writing JSON data to disk

What is the easiest way to write a JSON data/NSDictionary and read it back again? I know there's NSFileManager, but is there an open source framework library out there that would make this process easier? Does iOS5 NSJSONSerialization class supports writing the data to disk?
Yup, NSJSONSerialization is the way to go:
NSString *fileName = #"myJsonDict.dat"; // probably somewhere in 'Documents'
NSDictionary *dict = #{ #"key" : #"value" };
NSOutputStream *os = [[NSOutputStream alloc] initToFileAtPath:fileName append:NO];
[os open];
[NSJSONSerialization writeJSONObject:dict toStream:os options:0 error:nil];
[os close];
// reading back in...
NSInputStream *is = [[NSInputStream alloc] initWithFileAtPath:fileName];
[is open];
NSDictionary *readDict = [NSJSONSerialization JSONObjectWithStream:is options:0 error:nil];
[is close];
NSLog(#"%#", readDict);
It seems you must ensure that the directories in the path exist, otherwise your app will hang and consume 100% of your CPU when using + writeJSONObject:toStream:options:error:. The file itself will be created by the stream.

How to parse xml from specific tag

I have a xml file, i can do the parsing using the NSXML parser. Now i want to start parsing from specific tag , how this can be done ?
<RootElement>
<City>
<Name>WC</Name>
<Time>6 am</Time>
<Notes>Hi</Notes>
</City>
<State>
<Name>ABC</Name>
<Time>6 a.m.</Time>
<Time>2 a.m.</Time>
<Notes>This is a note for ABC</Notes>
</State>
<State>
<Name>DEF</Name>
<Time>8 am</Time>
<Time>10 am</Time>
<Notes>This is a note for DEF</Notes>
</State>
</RootElement>
Now the parsing should start from State tag , but it parses entire document and displays the name,time,notes from the City Tag also. Parsing from specified tag , how it can be done ?
Take a look at:
- (NSArray *)nodesForXPath:(NSString *)xpath error:(NSError **)error
EDIT: Code example
NSError* error;
CXMLDocument *doc = [[CXMLDocument alloc] initWithData:Data options:0 error:&error];
// figure out what nodes are in the doc, and pick it apart with the nodesForXPath call
NSArray * results = [doc nodesForXPath:#"/RootElement" error:&error];
NSString * finalUrl = self.defaultURL;
if ( [results count] > 0 ) {
NSString * element = [(CXMLElement *)[results objectAtIndex:0] stringValue];
if ( [element isEqualToString:#"State") {
// Do state processing
}
}
[doc release];

CXMLDocument - [CXMLNode nodeForXPath] problem?

I'm developing epub reader program.
To get contents of the epub file, I wanted to parse container.xml.
Here it is.
<?xml version="1.0"?>
<container version="1.0" xmlns="urn:oasis:names:tc:opendocument:xmlns:container">
<rootfiles>
<rootfile full-path="OPS/content.opf"
media-type="application/oebps-package+xml"/>
</rootfiles>
</container>
I wanted to get element, so I used following code.
CXMLDocument* manifestFile = [[[CXMLDocument alloc] initWithContentsOfURL:[NSURL fileURLWithPath:manifestFilePath] options:0 error:nil] autorelease];
NSArray* nodes = [manifestFile nodesForXPath:#"//rootfiles" error:nil];
NSLog(#"Nodes count is %d\n", [nodes count]);
And I get the following result.
Nodes count is 0
Why do I got this result?
Please help me.
Thanks.
The problem is that your XML has a namespace. You need to explicitly define the namespaces to use when querying with XPath.
NSDictionary *dict = [NSDictionary dictionaryWithObjectsAndKeys:
#"urn:oasis:names:tc:opendocument:xmlns:container",
#"oasis",
nil];
NSArray* nodes = [manifestFile nodesForXPath:#"//oasis:rootfiles" namespaceMappings:dict error:nil];
NSLog(#"Nodes count is %d\n", [nodes count]); // --> Nodes count is 1

Parsing SOAP result using TouchXML in iOS SDK (iPad)

I'm working on an iPad project that is using functions in a WebService.
Handling webservice connection, data etc works find. But i'm not able to parse the result SOAP using TouchXML. Getting the nodes out of the xml always returns 0 length.
The output xml:
<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"> <soap:Body> <LogOnResponse xmlns="http://coreservices.org/v1"> <LogOnResult> <Id>1c0e9ad0-a3be-4cd0-8f0d-0616a63a4e28</Id> <userId>2</userId> <user> <ID>2 <Name>Beheerder <emailAddress /> <cultureName>nl-NL</cultureName> </user> </LogOnResult> </LogOnResponse> </soap:Body></soap:Envelope>
parser code:
NSData *aData = [[NSData alloc] initWithBytes:[webData mutableBytes] length:[webData length]];
NSString *xmlData = [[NSString alloc] initWithData:aData encoding:NSASCIIStringEncoding];
NSLog(#"%#", xmlData);
[xmlData release];
CXMLDocument *domUserIdentity = [[[CXMLDocument alloc] initWithData:aData options:0 error:nil] autorelease];
[aData release];
NSArray *nodesList = [domUserIdentity nodesForXPath:#"//LogOnResult" error:nil]; // 0 length
for (CXMLElement *resultElement in nodesList) {
for (int counter = 0; counter
NSString *elemName = [[resultElement childAtIndex:counter] name];
NSString * elemValue = [[[resultElement childAtIndex:counter] stringValue] stringByTrimmingCharactersInSet: [NSCharacterSet whitespaceAndNewlineCharacterSet]];
[elemName release];
[elemValue release];
}
}
[nodesList release];
Any idea what did i do wrong?
Thank's alot in advance.
Inoel
Try using the NSXMLDocument, it should work as well as CXMLDocument. Here are some docs:
link
I recommend using an XPath Parser. You aren't doing anything too heavy, so the speed hit is well worth it. My personal choice is TFHpple. There are slightly faster and more precise solutions out there, but I find TFHpple's simplicity hard to beat.
Example:
NSData *data = [[NSData alloc] initWithContentsOfFile:#"example.html"];
// Create parser
xpathParser = [[TFHpple alloc] initWithXMLData:data];
NSArray *elements = [xpathParser search:#"//LogOnResult"];
TFHppleElement *element = [elements objectAtIndex:0];
// Get the text within the cell tag
NSString *content = [element content];
[xpathParser release];
[data release];
Obviously you'd have to write some code to save the parts you want. Looking at this data, I'd probably make a custom data holder class that you can access with properties. You can parse your XML into that and then save it as a property in the view controller that will be accessing it.
Happy coding!