Possible to validate xml against xsd using Objc/iP

2020-06-20 05:00发布

问题:

I have xml files that I read in at runtime, is it possible to validate the xml against an xsd file at runtime using Obj C?? This can be done in java and c#.. But i need do it run time in my iphone app.

回答1:

I don't think you can do this using Obj C on iOS. I think you'll need to use libxml2.

Here's an example of a simple C program that validates XML against XSD.

Here are instructions on adding libxml2 to an XCode project.



回答2:

CodeSynthesis XSD/e should support iPhone (inside the Mac package)


Edit #1: Setup instruction

To build the XSD/e runtime library (libxsde.a), perform the following steps:

  1. Unpack the pre-compiled XSD/e package for Mac OS X.

  2. Start a new terminal window and run the following commands:

    cd xsde-3.2.0-i686-macosx

    cp etc/ios/config-xcode.make config/config.make

    Don't close the terminal.

  3. Edit config/config.make and adjust the XSD/e configuration to suit your requirements.

  4. In the terminal, execute:

    cd libxsde

    make

    If the make command is not found, try /Developer/usr/bin/make (or your alternative XCode installation directory).

  5. Start XCode and perform the following steps:

    5.1 Select "File"->"New Project"

    5.2 In the opened dialog select "iOS Library"->"Cocoa Touch Static Library". Click "Choose...".

    5.3 In the next dialog type libxsde in the "Save As" field and navigate to the xsde-3.2.0-i686-macosx directory. Click "Save".

    5.4 Next you should see a warning dialog saying that the libxsde directory already exists. This is expected so click "Replace".

    5.5 In the project window in the "Groups & Files" list select "Other Sources" group, then select "Project"->"Add to Project...".

    5.6 In the opened dialog navigate to the xsde-3.2.0-i686-macosx/libxsde directory and select the src directory. Click "Add".

    5.7 In the next dialog leave the default settings and click "Add". Now you should see multiple source files (.cxx and .c) listed in the "Other Sources" group.

    5.8 Next select "Project"->"Edit Project Settings", "Build" tab. In the "Configurations" drop-down list select "All Configurations".

    5.9 Scroll down to the "Search Paths" section and add . (dot) to the "Header Search Paths" field.

    5.10 Scroll down to the "GCC 4.2 - Language" section and add the -fvisibility=hidden flag to the "Other C Flags" field as well as the -fvisibility=hidden and -fvisibility-inlines-hidden flags to the "Other C++ Flags" field. If you set any extra C/C++ flags in your application's project, you may also want to add them here.

    5.11 Build the project for all the desired configurations (for example, Debug/Release, Device/Simulator, ARMv6/ARMv7, etc).

  6. In the terminal window create "fat" libraries by running the following commands (which may need to be adjusted depending on the configurations that you have built):

    cd build

    lipo -output libxsde.a -create Release-iphonesimulator/liblibxsde.a Release-iphoneos/liblibxsde.a

    lipo -output libxsde-d.a -create Debug-iphonesimulator/liblibxsde.a Debug-iphoneos/liblibxsde.a

If at some point you need to change the XSD/e configuration then it is best to start from scratch (step 1 above) since the set of files that is added to the XCode project may vary from configuration to configuration.

Once the runtime library is built, to integrate XSD/e into your application perform the following steps:

  1. Compile your schemas to C++ with the XSD/e compiler (xsde-3.2.0-i686-macosx/bin/xsde) and add the resulting generated C++ files to your project.

  2. To link your application to the XSD/e runtime library (libxsde), perform the following steps in your project:

    2.1 In the "Targets" group, double-click on your application to open the "Info" dialog.

    2.2 Select the "General" tab and click on the Plus (+) button to add the library.

    2.3 In the opened dialog click the "Add Other..." button and add either the libxsde.a or libxsde-d.a (debug) fat library created above.

  3. To add the XSD/e runtime headers to your application's search paths, perform the following steps in your project:

    3.1 Select "Project"->"Edit Project Settings", "Build" tab. In the "Configurations" drop-down list select "All Configurations".

    3.2 Scroll down to the "Search Paths" section and add the path to the xsde-3.2.0-i686-macosx/libxsde directory to the "Header Search Paths" field.

    3.3 Build the application.



回答3:

there are no sdks for this in the objC Api for iOS. To do this you have to drop down to using libxml's C APIs directly.

  1. you load the xml (e.g. xmlReadMemory)
  2. then for XSD you'd use xmlSchemaValidateDoc

or you can use the following wrapper class [disclaimer: I am the author]:
DDXMLValidator (part of my helpers @ https://github.com/Daij-Djan/DDUtils/)

//validate
NSError *error = nil;
NSURL *schemaURL = [[NSBundle mainBundle] URLForResource:@"XMLSchema" withExtension:@"xsd"];
if(![[SNXMLValidator sharedInstace] validateXMLData:xmlData withSchema:SNXMLValidatorSchemaTypeXSD schemaFile:schemaURL error:&error]) {
    DebugLog(@"Failed to validate data: %@", error);
}

the class can do DTD, XSD, RelaxNG (it just wraps libxml2)