Sie sind auf Seite 1von 7

How-to guide: Parse a OData document using SDM Parser for OData based Android applications

Applies to:
This document and code samples provided here is valid for product Sybase Unwired Platform (SUP) - Online Data Proxy (ODP), release version 2.2.

Summary
This document describes how to use the SDMParser to parse OData XML while developing OData based Android applications. The document details the usage of various APIs provided by the SDMParser to read, create and update the OData for request-response operations. Author: Suhas Narasimhan

Company: SAP Labs India Pvt. Ltd. Created on: 26 November, 2012

Author Bio
Suhas Narasimhan is a Developer Associate in the SUP-ODP team under Mobility Platform largely working on the OData SDK for Android platform.

SAP COMMUNITY NETWORK 2012 SAP AG

scn.sap.com 1

How-to guide: Parse a OData document using SDM Parser for OData based Android applications

Table of Contents
Introduction ......................................................................................................................................................... 3 Initializing the SDMParser ............................................................................................................................... 3 Parsing Service Document ............................................................................................................................. 3 Parsing Metadata ............................................................................................................................................ 4 Parsing Entries ................................................................................................................................................ 5 Related Content .................................................................................................................................................. 6 Copyright............................................................................................................................................................. 7

SAP COMMUNITY NETWORK 2012 SAP AG

scn.sap.com 2

How-to guide: Parse a OData document using SDM Parser for OData based Android applications

Introduction
The SDMParser is responsible for transforming between the different representations of OData structures, for example, parsing from XMLs to a Java Object or building XMLs from a Java Object. The SDMParser component uses javax.xml.parsers.SAXParser as a parser engine, defining its own extension of org.xml.sax.helpers.DefaultHandler class as a handler for SAXParser. The outcome documents of SDMParser are all optimized for persistence using SDMPersistence, implementing the ISDMPersistable interface. Parsing related data is loaded during the initialization of the SDMParser component. This means that SDMParser must always be initialized before using the SDMParser documents. As a result of parsing, SDMParser provides Java Object representations of the appropriate OData structures. Each such SDMOData Java Object is a representation of the appropriate Data XML and provides dynamic access to all of its elements and attributes. Besides the full access with the dynamic method, OData Java Objects provide interfaces for a more convenient access of data used in the most common scenarios. This document describes the steps involved and the APIs to be used in case of parsing an OData document during the course of developing a OData based Android application.

The document consists of the following sections: Initializing the SDMParser Parsing Service Document Parsing Metadata Parsing Entries data

Initializing the SDMParser We need to first initialize the SDMParser java object in order to be able to parse the OData xmls during the request- response in the android application. SDMParser takes in object of SDMPreferences and SDMLogger as parameters. Following is the sample code:

// initializing the objects of SDMParser, SDMLogger and SDMPreferences public SDMParser parser; public SDMLogger logger; public SDMPreferences pref; logger = new SDMLogger(); pref = new SDMPreferences(getApplicationContext(), logger); parser = new SDMParser(pref, logger);

Parsing Service Document To parse the service document for a OData service, SDM Parser provides the API parseSDMODataServiceDocumentXML().

This API takes serviceDocument xml string as parameter and returns an object of the type ISDMODataServiceDocumnet.

SAP COMMUNITY NETWORK 2012 SAP AG

scn.sap.com 3

How-to guide: Parse a OData document using SDM Parser for OData based Android applications

Syntax: ISDMODataServiceDocument parseSDMODataServiceDocumentXML(String serviceDocumentXML);

Following is the sample code: public void onSuccess(ISDMRequest iRequest, ISDMResponse iResponse) { ISDMResponse response = iResponse; ISDMODataSerISDMODataServiceDocumentviceDocument serviceDocument; try { HttpEntity responseEntity = response.getEntity(); String serviceDoc = EntityUtils.toString(responseEntity); serviceDocument = parser.parseSDMODataServiceDocumentXML(serviceDoc); } } Parsing Metadata Once the service document is parsed we need to parse the metadata or the schema document associated with the service to be able to parse the various collections or entries in that service. Metadata document provides the structure of the data and the relationship between the various entities. SDM Parser provides an API to parse the OData metadata document. Syntax: ISDMODataSchema parseSDMODataSchemaXML(String schemaXML, ISDMODataServiceDocument serviceDocument); Following is the sample code: public void onSuccess(ISDMRequest iRequest, ISDMResponse iResponse) { ISDMResponse response = iResponse; ISDMODataSchema schema; try { HttpEntity responseEntity = response.getEntity(); String metadata = EntityUtils.toString(responseEntity); schema = parser.parseSDMODataSchemaXML(metadoc, serviceDocument); } }

SAP COMMUNITY NETWORK 2012 SAP AG

scn.sap.com 4

How-to guide: Parse a OData document using SDM Parser for OData based Android applications

Parsing Entries Once we have parsed the service document and the metadata document we can now parse the entries. To be able to parse the entries in the OData collections we can make use of the API parseSDMODataEntriesXML() provided in the SDMParser to parse the entries in the OData collection. This API takes the entries xml, the name of the collection to be parsed and the metadata parsed earlier as the parameters. Syntax: List<ISDMODataEntry> parseSDMODataEntriesXML(String entriesXML, String collectionId, ISDMODataSchema schema)

Following is the sample code: public void onSuccess(ISDMRequest iRequest, ISDMResponse iResponse) { ISDMResponse response = iResponse; String collectionName = SampleCollection; List<ISDMODataEntry> entries; try { HttpEntity responseEntity = response.getEntity(); String entriesXml = EntityUtils.toString(responseEntity); entries = parser.parseSDMODataEntriesXML(entriesXml,collectionName,schema); } }

SAP COMMUNITY NETWORK 2012 SAP AG

scn.sap.com 5

How-to guide: Parse a OData document using SDM Parser for OData based Android applications

Related Content
Developer Guide SDM Parser Sample Android Application on SUP 2.2

SAP COMMUNITY NETWORK 2012 SAP AG

scn.sap.com 6

How-to guide: Parse a OData document using SDM Parser for OData based Android applications

Copyright
Copyright 2012 SAP AG. All rights reserved. No part of this publication may be reproduced or transmitted in any form or for any purpose without the express permission of SAP AG. The information contained herein may be changed without prior notice. Some software products marketed by SAP AG and its distributors contain proprietary software components of other software vendors. Microsoft, Windows, Excel, Outlook, and PowerPoint are registered trademarks of Microsoft Corporation. IBM, DB2, DB2 Universal Database, System i, System i5, System p, System p5, System x, System z, System z10, System z9, z10, z9, iSeries, pSeries, xSeries, zSeries, eServer, z/VM, z/OS, i5/OS, S/390, OS/390, OS/400, AS/400, S/390 Parallel Enterprise Server, PowerVM, Power Architecture, POWER6+, POWER6, POWER5+, POWER5, POWER, OpenPower, PowerPC, BatchPipes, BladeCenter, System Storage, GPFS, HACMP, RETAIN, DB2 Connect, RACF, Redbooks, OS/2, Parallel Sysplex, MVS/ESA, AIX, Intelligent Miner, WebSphere, Netfinity, Tivoli and Informix are trademarks or registered trademarks of IBM Corporation. Linux is the registered trademark of Linus Torvalds in the U.S. and other countries. Adobe, the Adobe logo, Acrobat, PostScript, and Reader are either trademarks or registered trademarks of Adobe Systems Incorporated in the United States and/or other countries. Oracle is a registered trademark of Oracle Corporation. UNIX, X/Open, OSF/1, and Motif are registered trademarks of the Open Group. Citrix, ICA, Program Neighborhood, MetaFrame, WinFrame, VideoFrame, and MultiWin are trademarks or registered trademarks of Citrix Systems, Inc. HTML, XML, XHTML and W3C are trademarks or registered trademarks of W3C, World Wide Web Consortium, Massachusetts Institute of Technology. Java is a registered trademark of Oracle Corporation. JavaScript is a registered trademark of Oracle Corporation, used under license for technology invented and implemented by Netscape. SAP, R/3, SAP NetWeaver, Duet, PartnerEdge, ByDesign, SAP Business ByDesign, and other SAP products and services mentioned herein as well as their respective logos are trademarks or registered trademarks of SAP AG in Germany and other countries. Business Objects and the Business Objects logo, BusinessObjects, Crystal Reports, Crystal Decisions, Web Intelligence, Xcelsius, and other Business Objects products and services mentioned herein as well as their respective logos are trademarks or registered trademarks of Business Objects S.A. in the United States and in other countries. Business Objects is an SAP company. All other product and service names mentioned are the trademarks of their respective companies. Data contained in this document serves informational purposes only. National product specifications may vary. These materials are subject to change without notice. These materials are provided by SAP AG and its affiliated companies ("SAP Group") for informational purposes only, without representation or warranty of any kind, and SAP Group shall not be liable for errors or omissions with respect to the materials. The only warranties for SAP Group products and services are those that are set forth in the express warranty statements accompanying such products and services, if any. Nothing herein should be construed as constituting an additional warranty.

SAP COMMUNITY NETWORK 2012 SAP AG

scn.sap.com 7

Das könnte Ihnen auch gefallen