niji/xml-parser-bundle

Symfony 扩展包,使用 YAML 映射文件和 XPath 便捷解析 XML 文档。

安装: 206

依赖者: 0

建议者: 0

安全: 0

星标: 1

关注者: 6

分支: 0

类型:symfony-bundle

1.1 2018-11-29 16:33 UTC

This package is auto-updated.

Last update: 2024-09-05 02:16:30 UTC


README

本扩展包允许使用 XPath 便捷解析。

安装

composer req niji/xml-parser-bundle

XPath 解析

要解析 XPath 文件,您需要

  1. 创建一个类似以下的参数 YAML 文件
parameters:
  xml_parsers.mappings:
    mapping_a:
      destination_class: 'App\Entity\EntityName'
      base_root: '//namespace:XMLNode/XMLSubNode'
      mapping:
        property_dest: 'source_property_name'
        property_dest2: 'source_property2_name'
        sub_entity:
          destination_class: 'App\Entity\SubEntityName'
          base_root: 'XMLNodeName'
          mapping:
            sub_entity_property: 'source_subentity_property'
            ...
        ...

如果您的 XML 有 "" 作为默认命名空间,请在 XPath 查询中使用 "default" 作为命名空间名称。

destination_class 键是可选的,如果没有指定目标类,解析器将返回一个关联的 array 作为结果。

  1. Niji\XmlParserBundle\XmlParsingTrait 添加到您的目标类
<?php

namespace App\Entity;

use Niji\XmlParserBundle\XmlParsingTrait;

class EntityName
{
    
    use XmlParsingTrait;
    
    ...
}
  1. 在您的 Symfony 自定义代码中按照以下方式使用解析器
<?php

use Niji\XmlParserBundle\Parsers\XmlXPathParser;

class YourClass
{
    /**
    * @var \Niji\XmlParserBundle\Parsers\XmlXPathParser
    */
    protected $parser;
        
    /**
     * Inject the parser use Symfony's dependency injection.
     *
     * @param \Niji\XmlParserBundle\Parsers\XmlXPathParser $parser
     */
    public function __construct(XmlXPathParser $parser) {
        $this->parser = $parser;
    }
    
    /**
     * Parsing method.
     * 
     * @param string $sourceUrl
     * @param string $mappingName
     */
    public function yourMethod(string $sourceUrl, string $mappingName) {
        $xmlStr = file_get_contents($sourceUrl);
    
        // $result is a destination class or an array of destination classes or an associative array.
        $result = $this->parser->parse($xmlStr, $mappingName);
    }
}

处理器

有时您可能需要转换输入数据(例如:日期格式化、类型转换(字符串到布尔值)等)。

在这种情况下,您可以使用实现 XmlParsingProcessorInterfaceProcessor 类。

<?php

namespace App\XmlProcessors;

use Niji\XmlParserBundle\Processor\XmlParsingProcessorInterface;

class BooleanProcessor implements XmlParsingProcessorInterface 
{

    /**
     * Process the passed value
     *
     * @param mixed $value
     *   Source value.
     * @param array $config
     *   Processor configuration.
     *
     * @return mixed
     *   Processed value.
     */
    public function process($value, array $config = []) {
        $processedValue = null;
        
        // Do whatever you need to process the $value
        // e.g: $processedValue = (boolean)$value;
        
        return $processedValue;
    }
}

然后在映射中指定您的处理器的完全限定名称,如下所示

parameters:
  xml_parsers.mappings:
    mapping_a:
      destination_class: 'App\Entity\EntityName'
      base_root: '//namespace:XMLNode/XMLSubNode'
      mapping:
        property_dest:
          source: 'source_property_name'
          processor: 'App\XmlProcessors\BooleanProcessor'
          config:
            config_key: 'config_value'
            config_key2: 'config_value2'
            ...