You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

寻求可实现PHP对象与XML节点映射的Composer扩展包

Absolutely! If you're looking for a PHP equivalent to Java's JAXB—something that maps plain PHP objects to XML nodes (and vice versa) with extensible XML handling—there are several excellent Composer packages to choose from. Here are the top options I recommend:

Top PHP Composer Packages for XML-Object Mapping (JAXB Equivalent)

1. jms/serializer

This is one of the most popular and flexible tools in the PHP ecosystem for this use case. It supports both XML and JSON serialization/deserialization, and lets you define mappings between objects and XML via annotations or configuration files—just like JAXB.

  • Key features:
    • Annotations like @XmlRootElement, @XmlElement, and @XmlAttribute for intuitive mapping
    • Custom type handlers for complex data structures (e.g., dates, enums)
    • Highly extensible architecture—easily add custom serializers/deserializers
    • Handles nested objects, collections, and inheritance seamlessly
  • Basic usage example:
    use JMS\Serializer\Annotation as Serializer;
    
    /**
     * @Serializer\XmlRoot("customer")
     */
    class Customer {
        /**
         * @Serializer\XmlAttribute
         */
        public int $id;
    
        /**
         * @Serializer\XmlElement(cdata=false)
         */
        public string $fullName;
    }
    
    // Initialize serializer
    $serializer = \JMS\Serializer\SerializerBuilder::create()->build();
    
    // Deserialize XML string to PHP object
    $customer = $serializer->deserialize($xmlInput, Customer::class, 'xml');
    
    // Serialize PHP object back to XML
    $xmlOutput = $serializer->serialize($customer, 'xml');
    

2. symfony/serializer

Part of the Symfony component family, this package is lightweight, well-maintained, and works great both within Symfony projects and standalone. It offers robust normalization/denormalization between objects and XML.

  • Key features:
    • Uses attributes (or annotations for older PHP versions) to define XML mappings
    • Supports XML attributes, nested elements, and collection types
    • Extensible with custom normalizers and encoders
    • Integrates smoothly with other Symfony tools if you're using them
  • Basic usage example:
    use Symfony\Component\Serializer\Annotation\XmlRoot;
    use Symfony\Component\Serializer\Annotation\XmlAttribute;
    
    /**
     * @XmlRoot("product")
     */
    class Product {
        /**
         * @XmlAttribute
         */
        public string $sku;
    
        public string $productName;
    }
    
    // Set up serializer
    $serializer = new \Symfony\Component\Serializer\Serializer([
        new \Symfony\Component\Serializer\Normalizer\ObjectNormalizer(),
    ], [
        new \Symfony\Component\Serializer\Encoder\XmlEncoder(),
    ]);
    
    // Deserialize XML to object
    $product = $serializer->deserialize($xmlInput, Product::class, 'xml');
    
    // Serialize object to XML
    $xmlOutput = $serializer->serialize($product, 'xml');
    

3. doctrine/oxm

Doctrine's Object-XML Mapper is a solid choice, especially if you're already familiar with Doctrine ORM. It's built specifically for XML mapping and follows patterns similar to Doctrine's database ORM.

  • Key features:
    • Uses annotations or XML mapping files to define object-xml relationships
    • Supports complex mappings like embedded objects, collections, and inheritance
    • Integrates with Doctrine's metadata system for consistent configuration
  • Basic usage example:
    use Doctrine\OXM\Mapping\Annotations as OXM;
    
    /**
     * @OXM\XmlEntity(xml="order")
     */
    class Order {
        /**
         * @OXM\XmlAttribute(type="integer")
         */
        public int $orderId;
    
        /**
         * @OXM\XmlField(type="string")
         */
        public string $status;
    }
    
    // Configure OXM
    $config = \Doctrine\OXM\Configuration::create();
    $config->setMetadataDriverImpl($config->newDefaultAnnotationDriver([__DIR__.'/Entities']));
    $oxm = \Doctrine\OXM\OXM::create($config);
    
    // Deserialize XML to object
    $order = $oxm->marshalXmlToObject($xmlInput, Order::class);
    
    // Serialize object to XML
    $xmlOutput = $oxm->marshalObjectToXml($order);
    

Honorable Mention: php-xml-mapper/php-xml-mapper

If you need a simpler, lightweight alternative without all the extra features of the larger packages, this is a great pick. It focuses solely on XML-object mapping with minimal setup.

  • Key features:
    • Simple attribute-based mapping
    • Support for nested objects and arrays
    • No external dependencies beyond PHP itself

All these packages are installable via Composer (e.g., composer require jms/serializer) and are fully extensible, so you can tailor the mapping logic to match your server's specific XML structure requirements—just like you would with JAXB in Java.

内容的提问来源于stack exchange,提问作者mahkill

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.19 07:15:05