Django-tastypie -- how to generate customized xml - tastypie

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.

Related

SAPUI5 - OData calls fired fired internally by data.js when using odataModel

I am using sap.ui.model.odata.ODataModel to make service calls to the backend and bind data to a UI.
The coding is pretty basic. What I don't understand is the calls that the internal data.js makes to the server. When I do the following
var oDModel = new ODataModel(this.sapServiceURL + "/sap/opu/odata/sap/ZKMDOCUMENTS_SRV", {
json: true,
useBatch: false,
defaultCountMode: sap.ui.model.odata.CountMode.None,
skipMetadataAnnotationParsing: true
});
I can see two URLs being fired one of which fails every time. Please see below:
The first is the metadata call which I understand. I have no idea about the second rest call (always fails with 400 error).
The second URL being formed is
{hostURI}/sap/opu/odata/sap/ZKMDOCUMENTS_SRV/?$skip=0&$top=20
What is the second call for?
Why is the URL being formed
Is there a way to avoid this call since this is creating a malformed
URL anyways
Any help is appreciated.
Thanks.
That does not happen per say because of the OData model. The OData model creates requests based on either the bindings that you have made (declaratively or procodurally) or based on procedural ODataModel.read calls.
Based on the request format that you have there, my guess is that somewhere in your views or fragments you have bound an aggregation to the "/" path in the model. Example:
<List items="{/}">
<StandardListItem />
</List>
Alternatively, you might have a relative binding (e.g. items="{}") for an aggregation and the parent / ancestor context to be "/". Example:
<Panel binding="{/}" > <!-- most likely the panel binding is done via code -->
<List items="{}">
<StandardListItem />
</List>
<Panel>
I don't think that the request could be because of procedural ODataModel.read calls, because of the fact that it requested a paged (skip = 0 and top = 20) portion of the aggregation. Actually, purely based on these two numbers, I would say that you have a List with growing = "true" somewhere in your views or fragments (and this list is causing the request).

How to add a node to an existing xml file using iphone?

In my application I need to add new element to parent node.
for Example My Xmlfile:
<person>
<Name>Thrinath</Name>
<Add>Hyd</Add>
</person>
Now I need to add new element named sal to person parent node
<person>
<Name>Thrinath</Name>
<Sal>50000</Sal>
<Add>Hyd</Add>
</person>
I know that to read XML file we have XML parser,but I dont know how to write into the XML file like my requirement.
Use some kind of DOM model to modify the XML, see for example this link.

NSXMLParser with multiple delegates

I'm trying to parse an xml tree file whith multiple NSXMLParserDelegate parsers but I'm getting in the next issue.
My XML structure is something like that.
<Object1>
<Name>Ricky</Name>
<Surname>Woodstock</Surname>
<Adress>
<City>Los Angeles</City>
<State>California</State>
<Country>USA</Country>
</Adress>
<Items>
<Item>
<Id>1</Id>
<Description>Sports Bag</Description>
<Price>13.45</Price>
</Item>
<Item>
<Id>2</Id>
<Description>Baseball Cap</Description>
<Price>6.90</Price>
</Item>
</Items>
<Total>20.15</Total>
</Object1>
And my Issue is that when I changes delegates, it starts parsing not in top-level tag, instead it begins parsing on the first nested tag.
For example.
I begin parsing XML wih XMLObject parser which parses element
When it reaches Adresss tag I set delegate to XMLAdressParser (and set XMLAdressParser's parent to self for returning) but XMLAdressParser begins parsing on tag.
And almost with Items and Item tags.
I think it's normal cause of a Parser definition, but that's the question:
Is there any way for the delegate to start parsing in correspondint tag, like first one?
Thanks in advance for any help.
Sergio
Rather than using multiple delegates I suggest using a single delegate and having it distribute the work as you feel appropriate.

NSXMLParser with multiple attributes

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

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.