定制 thenextcoder/xml-flow 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

thenextcoder/xml-flow

Composer 安装命令:

composer require thenextcoder/xml-flow

包简介

A simple, fast, lightweight, and easy-to-use PHP library for building, parsing, and validating XML documents.

README 文档

README

License

XMLFlow is a simple, fast, lightweight, and easy-to-use PHP library for building, parsing, and validating XML documents. It can also be used to build highly structured prompts for LLM.

Features

  1. Build XML documents programmatically with XmlBuilder
  2. Parse any XML data into PHP arrays or objects
  3. Validate the syntax and structure of your XML data

Installation

You can install XMLFlow via Composer:

composer require thenextcoder/xml-flow

You will then be able to import XMLFlow in your PHP scripts like this:

use TheNextCoder\XmlFlow\Builder\XmlBuilder;

Usage XmlBuilder

Example 1: Creating a Simple Document

This example demonstrates how to create a simple XML document with a custom root element and a few child elements.

use TheNextCoder\XmlFlow\Builder\XmlBuilder;

$xmlBuilder = new XmlBuilder('greeting');
$xmlBuilder->addElement('hello', 'World');
$xmlBuilder->addElement('goodbye', 'See you later');

echo $xmlBuilder->getFormattedXml();

Output:

<?xml version="1.0" encoding="UTF-8"?>
<greeting>
  <hello>World</hello>
  <goodbye>See you later</goodbye>
</greeting>

Example 2: Nested Elements with Attributes

This example shows how to create an XML document with nested elements and attributes, illustrating the use of XPath to specify the parent element.

$xmlBuilder = new XmlBuilder('book', ['isbn' => '000-0-00-000000-0']);
$chapter = $xmlBuilder->addElement('chapter', 'Introduction to XML', null, ['number' => '1']);
$xmlBuilder->addElement('section', 'Basics of XML', $chapter, ['id' => 'section-1']);

echo $xmlBuilder->getFormattedXml();

Output:

<book isbn="000-0-00-000000-0">
  <chapter number="1">Introduction to XML
    <section id="section-1">Basics of XML</section>
  </chapter>
</book>

Example 3: Using XPath to Add Elements

Illustrates adding elements to a specified parent using XPath, useful for more complex document structures.

$xmlBuilder = new XmlBuilder('library');
$xmlBuilder->addElement('shelf', null, null, ['id' => 'shelf-1']);
$xmlBuilder->addElement('book', 'XML for Dummies', '//shelf[@id="shelf-1"]', ['author' => 'John Doe']);

echo $xmlBuilder->getFormattedXml();

Output:

<library>
  <shelf id="shelf-1">
    <book author="John Doe">XML for Dummies</book>
  </shelf>
</library>

Example 4: Complex Document Creation

This example creates a more complex XML document, demonstrating the class's flexibility.

$xmlBuilder = new XmlBuilder('catalog');
$products = $xmlBuilder->addElement('products');
for ($i = 1; $i <= 3; $i++) {
    $product = $xmlBuilder->addElement('product', "Product $i", $products);
    $xmlBuilder->addElement('price', '$' . (10 * $i), $product, ['currency' => 'USD']);
}

echo $xmlBuilder->getFormattedXml();

Output:

<catalog>
  <products>
    <product>
      <price currency="USD">$10</price>Product 1
    </product>
    <product>
      <price currency="USD">$20</price>Product 2
    </product>
    <product>
      <price currency="USD">$30</price>Product 3
    </product>
  </products>
</catalog>

Usage XmlParser

Example 1: Parsing XML Data

This example demonstrates how to parse an XML string into a PHP array.

use TheNextCoder\XmlFlow\Parser\XmlParser;

$xmlString = <<<XML
<task>
    <title>Write a documentation</title>
    <priority>High</priority>
    <subtasks>
        <subtask>Outline the main sections and subtopics</subtask>
        <subtask>Write the introductory overview</subtask>
        <subtask>Draft the "getting started" or installation guide</subtask>
        <subtask>Detail the main functionalities and their uses</subtask>
        <subtask>Explain any advanced features or options</subtask>
        <subtask>Write troubleshooting tips or FAQs section</subtask>
        <subtask>Include screenshots, diagrams, or other visual aids</subtask>
        <subtask>Address any known issues or limitations</subtask>
        <subtask>Indicate on how users can submit comments or questions</subtask>
        <subtask>Proofread for clarity, accuracy, and grammar</subtask>
        <subtask>Solicit feedback from colleagues or beta testers</subtask>
        <subtask>Make necessary revisions based on feedback</subtask>
        <subtask>Finalize and publish the documentation</subtask>
    </subtasks>
</task>
XML;

$parser = new XmlParser();

try {
    $xml = $parser->parse($xmlString);
    
    // Extracting title and priority
    echo "Task: " . $xml->title . "\n";
    echo "Priority: " . $xml->priority . "\n\n";
    
    // Extracting and listing subtasks
    echo "Subtasks:\n";
    foreach ($xml->subtasks->subtask as $subtask) {
        echo "- " . $subtask . "\n";
    }

    // Optional: Converting to an associative array
    $arrayRepresentation = $parser->toArray($xml);
    echo "\nArray Representation:\n";
    print_r($arrayRepresentation);

} catch (Exception $e) {
    echo "Error: " . $e->getMessage() . "\n";
}

Usage XmlValidator

Example 1: Validating XML Data

This example demonstrates how to validate the syntax and structure of an XML string.

use TheNextCoder\XmlFlow\Validator\XmlValidator;

$xmlContent = '<root><child>Example</child></root>'; // Your XML content here

try {
    XmlValidator::validate($xmlContent);
    echo "The XML is well-formed.";
} catch (Exception $e) {
    echo "The XML is not well-formed. Errors: " . $e->getMessage();
}

Contributing

Contributions are welcome! Please see CONTRIBUTING.md for more details.

License

XMLFlow is open-source software licensed under the MIT license.

thenextcoder/xml-flow 适用场景与选型建议

thenextcoder/xml-flow 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 502 次下载、GitHub Stars 达 0, 最近一次更新时间为 2024 年 03 月 22 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「xml」 「xml builder」 「XML Validator」 「xml parser」 「llm xml prompt」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 thenextcoder/xml-flow 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 thenextcoder/xml-flow 我们能提供哪些服务?
定制开发 / 二次开发

基于 thenextcoder/xml-flow 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 502
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 20
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 1
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2024-03-22