NSXMLParser with multiple attributes - iphone

I have the following XML (doing an app for iPhone):
<Row>
<Field name="employee_id_disp">00070431</Field>
<Field name="given_name">John</Field>
<Field name="family_name">Doe</Field>
</Row> ...
How can I retrieve values only for one of the attributes, for example value "John" for attribute name="given_name" ?
Thanks for answers.

Presumably, you'll be using NSXMLParser to parse this.
That means in your didStartElement: delegate callback, you should check the passed attributes dictionary to see if it has a key "name" with a value "given_name". If it does, you'll want to set some flag that you can start recording the characters found in foundCharacters: callback. Once you reach the didEndElement: callback for the element "Field", you'll have aggregated all the characters into a string, and that string is the name. (Most likely, the characters will all come be reported in one callback to foundCharacters:, but that's not guaranteed).

Easiest way: use an XPath processor and evaluate for the expression:
"/Row/Field[#name='given_name']/text()"
It should return "John" as the result. You can try it in this simulator here: http://www.mizar.dk/XPath/
Here's a simple way to get access to the XPath that comes with libxml2: http://cocoawithlove.com/2008/10/using-libxml2-for-parsing-and-xpath.html
And a good resource for finding which XML parsers have XPath support: http://www.raywenderlich.com/553/how-to-chose-the-best-xml-parser-for-your-iphone-project

Related

How to provide field type in XML datasource of ireport designer?

I am newbie to the ireport designer.This question may simple for you.I have tried to XML file as data source and it worked.The problem is All the fields are coming as java.lang.string. How to provide the field type in XML datasource.Is it possible to provide field type in XML file itself. Consider
<customer>
<name>obuli</name>
<age><22></age>
<subscriber>
<name>sundar</name>
<no_of_transactions>100</no_of_transactions>
</subscriber>
</customer>
Here the customer and subscriber are classes so i need to provide filed type com.test.Customer,com.test.Subscriber. I can achieve this by java bean datasource. But still i need the XML datasource way.
Is it possible to set the field type in XML datasource way ?
I do not think so. I also do not think you need that feature as reports only require scalar values. So just map the scalar values in your datasource to your scalar valued report fields.
You have to go on the XML code of your document and then search your field tag name, for example:
<field name="xxxx" class="java.lang.String"/>
change to Date type:
<field name="xxxx" class="java.util.Date"/>

how to parse malformed HTML using HTML::Parser

I am trying to parse an HTML with meta-tag as :
<meta name="id" content=""12345.this.is.a.sample:id:required.67890"#abc.com">
The html::parser returns this "" empty value instead of the actual value required. This is my code depicting the start event handler:
sub start {
my ($self, $tagname, $attr, $attrseq, $origtext) = #_;
if ($tagname eq 'meta') {
print "Meta found: ", $attr->{ name }, $attr->{content}, "\n";
}
}
Any ideas on how to get the required value?
I think a quote from Charles Babbage is appropriate here:
On two occasions I have been asked, — “Pray, Mr. Babbage, if you put into the machine wrong figures, will the right answers come out?” […] I am not able rightly to apprehend the kind of confusion of ideas that could provoke such a question.
– Passages from the Life of a Philosopher (1864)
This is known as the Garbage-In, Garbage-Out (GIGO) principle. In your case, you have malformed HTML. If you feed that to an HTML parser, you'll necessarily get bogus output. The HTML standard is already quite lax to deal with all kinds of common errors, but your example is much more broken.
There is, of course, one solution: Don't treat your input as HTML, but as some derived format where your example happens to be legal input. You'd have to write a custom parser of your own or adapt an existing HTML parser to your needs, but that would work.
However, I think that fixing the source of the input would be easier than writing your own parser. All that is needed is the quotes inside the attribute to be escaped, or for the attribute to use single quotes:
<meta name="id" content=""12345.this.is.a.sample:id:required.67890"#abc.com">
<meta name="id" content='"12345.this.is.a.sample:id:required.67890"#abc.com'>
Okay, found out a way to get the content for this particular problem. The $origtext variable above gets the value of the argspec identifier text which is defined in the documentation as :
Text causes the source text (including markup element delimiters) to be passed.
So, basically,
print $origtext;
would give me the source text as output:
<meta name="id" content=""12345.this.is.a.sample:id:required.67890"#abc.com">
I can use a regex to exploit this value contained in $origtext and get the desired stuff.

Django-tastypie -- how to generate customized xml

I have ModelResource in tastypie for a Simple Model that has an id and a name.
The XML output is shown below. But I would like to use my model name in place of "object". I seem to endlessly struggle on how to solve this -- any quick help on this? Thanks a million-- really!
<response>
<objects type="list">
<object>
<id>1</id>
<name>foo1</name>
<resource_uri> blah </resource_uri>
</object>
<object>
<id>2</id>
<name>foo2</name>
<resource_uri> blah </resource_uri>
</object>
</objects>
</response>
You could try overloading the "alter_list_data_to_serialize(...)" method of the Resource class. As the docstring says:
A hook to alter list data just before it gets serialized & sent to the user.
Useful for restructuring/renaming aspects of the what's going to be sent.
So that's the place when you change the "data" which soon after gets transformed into XML and sent to the user.
Something like this should get you going:
def alter_list_data_to_serialize(self, request, data):
#self.object_class.lower() is the name you'd like instead of "objects"
data = { self.object_class.lower() : [ element for element in data[ 'objects' ] ] }
return data
I haven't tested this, but I'm using this method to alter how my responses look succesfully.

What are some handy tricks for submitting Grails forms?

Everybody's aware of passing parameters to a controller via a html form:
<g:form action="save">
<g:textField name="text1" />
</g:form>
And I'm vaguely aware of being able to structure these parameters into some sort of object notation in Grails:
<g:form action="save">
<g:textField name="text.a" />
<g:textField name="text.b" />
</g:form>
With very little idea how they are structured in the controller (objects? hashmaps? I recall having to use .value at some point using the latter example).
So I guess this question is really two questions:
How does Grails handle parameters in object notation like the second example? Can you stick them into arrays too?
What are some other tricks regarding form submission and its parameters that can make forms with very complex and iterative data trivial to handle in the controller? For instance, ATG allows you to bind form fields to beans and walk its entire property graph to find the property you need to set.
The second notation "text.a" is used to disambiguate data conversion from properties to domain objects. For example, if you have 2 domain objects each with a property "a", if you do domObj1.properties = params and domObj2.properties = params the value will go to both domain objects which may not be what you want. So in your view you should have variables domObj1.a and domObj2.a and in your grails controller you can instantiate using def domObj1 = new DomObj1(params["domObj1"])
By your second question if you mean whether you can iterate over objects, you very well can, using GPath syntax in a ${} wrapper, for e.g check out the code in the id property below.
<td><g:remoteLink controller="device" action="getDevice" id="${objInstance.prop1.prop2.id}" update="propDetail">${fieldValue(bean: objInstance.prop1, field: "prop1")}</g:remoteLink></td>
The example above also shows an ajax way of form submission from grails gsp.

iPhone : parse Lengthy XML file on the Base of Key Field

i want to parse the xml File. xml File structure is following
<?xml version="1.0" encoding="utf-8"?>
<Level>
<p id='327'>
<Item>
<Id>5877</Id>
<Type>0</Type>
<Icon>---</Icon>
<Title>Btn1Item1</Title>
</Item>
<Item>
<Id>5925</Id>
<Type>0</Type>
<Icon>---</Icon>
<Title>Btn1Item4</Title>
</Item>
</p>
<p id='328'>
<Item>
<Id>5878</Id>
<Type>0</Type>
<Icon>---</Icon>
<Title>Btn2Item1</Title>
</Item>
<Item>
<Id>5926</Id>
<Type>0</Type>
<Icon>---</Icon>
<Title>Btn2Item4</Title>
</Item>
</p>
</Level>
in above code there are only 2 tag for <p>. but in actual there are multiple tag. i want to search the specific tag for which attribute id have some specific value (say 327).
so one way is that i parse the XML file from start to get the desired result. whether there are any other method from which i can direct locate the desired tag. for example if i want to search the <p> tag in above XML for attribute id =328, then it does not parse the id=327 and direct return only those item which are related to id=328
Please suggest
Depends how you define "parse".
A "quick & dirty" (and potentially buggy) way would be to find the fragment using a regex search (or a custom parser) first, then feed the fragment to a true XML parser. I don't know of anything that would do this for you, you'd have to roll it yourself. I would suggest that it's not the way to go.
The next level is to feed it through a SAX-like parser (which NSXMLParser is a form of).
In your handler for the <p> element, check the id attribute and if it matches your value (or values), set a flag to indicate if child elements should be interpreted.
In your child element handlers, just check that flag first (in a raw NSXMLParser handler all elements would go to the same method, of course).
So it's true that NSXMLParser would be parsing the whole document - but just to do the minimal work to establish the correct XML parser context. The real work of handling the elements would be deferred until the value is met. I don't see any way around that without something hacky like the regex suggestion.
If this is too much overhead I'd reconsider whether XML is the right serialization format for you (assuming you have any control over that)?
If you do stick with NSXMLParser, my blog article here might help to at least make the experience nicer.
The libxml2 library can receive XPath queries with the following extensions. With these extensions you might issue the XPath query /p[#id = "328"] to retrieve that specific node's children.