使用XML和YAML文件的 文件输入和输出

本文详细介绍了如何使用OpenCV进行XML和YAML文件的输入输出操作,包括文件打开关闭、基本类型和OpenCV数据结构的读写,以及自定义数据结构的序列化方法。示例代码涵盖C++和Python两种语言,展示了如何处理序列和映射类型的输入输出。
摘要由CSDN通过智能技术生成

英文链接:File Input and Output using XML and YAML files

文章目录

目标

  • 如何打印和读取文本条目到文件 和 OpenCV使用YAML或XML文件?
  • 如何为OpenCV数据结构做同样的事情?
  • 如何为你的数据结构做到这一点?
  • 使用OpenCV数据结构,如cv::FileStorage, cv::FileNodecv::FileNodeIterator

源码

#include <opencv2/core.hpp>
#include <iostream>
#include <string>
using namespace cv;
using namespace std;
static void help(char** av)
{
   
    cout << endl
        << av[0] << " shows the usage of the OpenCV serialization functionality."         << endl
        << "usage: "                                                                      << endl
        <<  av[0] << " outputfile.yml.gz"                                                 << endl
        << "The output file may be either XML (xml) or YAML (yml/yaml). You can even compress it by "
        << "specifying this in its extension like xml.gz yaml.gz etc... "                  << endl
        << "With FileStorage you can serialize objects in OpenCV by using the << and >> operators" << endl
        << "For example: - create a class and have it serialized"                         << endl
        << "             - use it to read and write matrices."                            << endl;
}
class MyData
{
   
public:
    MyData() : A(0), X(0), id()
    {
   }
    explicit MyData(int) : A(97), X(CV_PI), id("mydata1234") // explicit to avoid implicit conversion
    {
   }
    void write(FileStorage& fs) const                        //Write serialization for this class
    {
   
        fs << "{" << "A" << A << "X" << X << "id" << id << "}";
    }
    void read(const FileNode& node)                          //Read serialization for this class
    {
   
        A = (int)node["A"];
        X = (double)node["X"];
        id = (string)node["id"];
    }
public:   // Data Members
    int A;
    double X;
    string id;
};
//These write and read functions must be defined for the serialization in FileStorage to work
static void write(FileStorage& fs, const std::string&, const MyData& x)
{
   
    x.write(fs);
}
static void read(const FileNode& node, MyData& x, const MyData& default_value = MyData()){
   
    if(node.empty())
        x = default_value;
    else
        x.read(node);
}
// This function will print our custom class to the console
static ostream& operator<<(ostream& out, const MyData& m)
{
   
    out << "{ id = " << m.id << ", ";
    out << "X = "
在Java中,将字符串形式的多级YAML文件转换成实际的YAML文件通常涉及到两个步骤:解析字符串并构建YAML对象,然后将这个对象序列化回YAML格式。你可以使用开源库如SnakeYAML或者Jackson YAML来完成这样的任务。 首先,你需要添加相应的依赖到你的项目中。例如,如果你选择SnakeYAML,你可以通过Maven添加如下依赖: ```xml <dependency> <groupId>org.yaml</groupId> <artifactId>snakeyaml</artifactId> <version>1.36</version> </dependency> ``` 以下是基本的步骤: 1. **解析字符串到YAML对象**: 使用`org.yaml.snakeyaml.Yaml`类的`load()`方法,传入你的字符串输入,它会返回一个匹配的YAML对象(可能是`Map<String, Object>`或自定义的数据结构)。 ```java Yaml yaml = new Yaml(); Map<String, Object> yamlObject = yaml.load(yourStringInput); ``` 2. **构建多级YAML结构**: 根据YAML对象的层次结构来创建嵌套的Java对象,这可能需要递归或者手动处理键值对。 3. **生成新的YAML字符串**: 如果已经有一个Java对象,可以使用`Dumper`类将其转换回YAML字符串。 SnakeYAML有`DumperOptions`用于配置输出格式。 ```java DumperOptions options = new DumperOptions(); Yaml dumper = new Yaml(options); String formattedOutput = dumper.dump(yamlObject); ``` 4. **保存到文件**: 最后,你可以将`formattedOutput`写入到一个新的文件中,使用Java的I/O操作。 记得在实际应用中处理可能出现的错误,比如无效的YAML语法或者缺少必要的数据结构映射。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值