可以在运行时使用 Objc/iPhone 代码针对 xsd 验证 xml

发布于 12-28 04:41 字数 344 浏览 1 评论 0原文

我有在运行时读入的 xml 文件,是否可以使用 Obj C 在运行时根据 xsd 文件验证 xml?这可以在 java 中完成c# ..但我需要这样做我的 iPhone 应用程序中的运行时间。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

饮惑2025-01-04 04:41:55

我认为你不能在 iOS 上使用 Obj C 来做到这一点。我认为您需要使用 libxml2。

这是一个验证简单 C 程序的示例XML 与 XSD 的对比。

以下是有关将 libxml2 添加到 XCode 的说明项目。

怎会甘心2025-01-04 04:41:55

CodeSynthesis XSD/e
应支持 iPhone(在 Mac 包内)


编辑 #1:安装说明

要构建 XSD/e 运行时库 (libxsde.a),请执行以下步骤:

  1. 解压适用于 Mac OS X 的预编译 XSD/e 包.

  2. 启动一个新的终端窗口并运行以下命令:

    cd xsde-3.2.0-i686-macosx

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

    不要关闭终端。

  3. 编辑config/config.make并调整XSD/e配置以适合您的
    要求。

  4. 在终端中,执行:

    cd libxsde

    制作

    如果找不到 make 命令,请尝试 /Developer/usr/bin/make (或您的

  5. 启动 XCode 并执行以下步骤:

    5.1 选择“文件”->“新建项目”

    5.2 在打开的对话框中选择“iOS Library”->“Cocoa Touch Static”
    库”。单击“选择...”。

    5.3 在下一个对话框中,在“另存为”字段中输入 libxsde 并导航
    到 xsde-3.2.0-i686-macosx 目录。点击“保存”。

    5.4 接下来您应该看到一个警告对话框,指出 libxsde 目录
    已经存在。这是预期的结果,因此请单击“替换”。

    5.5 在项目窗口的“组和文件”列表中选择“其他
    Sources”组,然后选择“Project”->“Add to Project...”。

    5.6 在打开的对话框中导航到 xsde-3.2.0-i686-macosx/libxsde 目录并
    选择src目录。单击“添加”。

    5.7 在下一个对话框中保留默认设置并单击“添加”。现在
    您应该看到多个源文件(.cxx 和 .c)列在
    “其他来源”组。

    5.8 接下来选择“项目”->“编辑项目设置”、“构建”选项卡。在
    “配置”下拉列表选择“所有配置”。

    5.9 向下滚动到“搜索路径”部分并添加 . (点)到
    “标题搜索路径”字段。

    5.10 向下滚动到“GCC 4.2 - 语言”部分并添加
    -fvisibility=“其他 C 标志”字段的隐藏标志
    作为 -fvisibility=hidden 和 -fvisibility-inlines-hidden 标志
    到“其他 C++ 标志”字段。如果您设置任何额外的 C/C++ 标志
    在您的应用程序项目中,您可能还想在此处添加它们。

    5.11 为所有所需的配置构建项目(例如,
    调试/发布、设备/模拟器、ARMv6/ARMv7 等)。

  6. 在终端窗口中通过运行以下命令创建“胖”库
    命令(可能需要根据配置进行调整)
    您已经构建的):

    cd 构建

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

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

如果在某些时候您需要更改 XSD/e 配置,那么它是 最好的
从添加的文件集开始(上面的步骤 1)
XCode 项目的配置可能因配置而异。

构建运行时库后,将 XSD/e 集成到您的应用程序中
执行以下步骤:

  1. 使用 XSD/e 编译器 (xsde-3.2.0-i686-macosx/bin/xsde) 将架构编译为 C++
    并将生成的 C++ 文件添加到您的项目中。

  2. 要将您的应用程序链接到 XSD/e 运行时库 (libxsde),请执行
    在您的项目中执行以下步骤:

    2.1 在“目标”组中,双击您的应用程序以打开
    “信息”对话框。

    2.2 选择“常规”选项卡,然后单击加号 (+) 按钮添加
    图书馆。

    2.3 在打开的对话框中单击“添加其他...”按钮并添加
    上面创建的 libxsde.a 或 libxsde-da(调试)胖库。

  3. 要将 XSD/e 运行时标头添加​​到应用程序的搜索路径,
    在您的项目中执行以下步骤:

    3.1 选择“项目”->“编辑项目设置”、“构建”选项卡。在
    “配置”下拉列表选择“所有配置”。

    3.2 向下滚动到“搜索路径”部分并将路径添加到
    将 xsde-3.2.0-i686-macosx/libxsde 目录复制到“标题搜索路径”字段。

    3.3 构建应用程序。

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.

请你别敷衍2025-01-04 04:41:55

iOS 的 objC Api 中没有为此提供的 sdks。为此,您必须直接使用 libxml 的 C API。

  1. 您加载 xml(例如 xmlReadMemory),
  2. 然后对于 XSD,您可以使用 xmlSchemaValidateDoc

或者您可以使用以下包装类 [免责声明:我是作者]:
DDXMLValidator(我的助手的一部分@ 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);
}

该类可以执行 DTD、XSD、RelaxNG(它只是包装 libxml2)

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)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文