9

Is there a PHP equivalent to JAXB? It's proved very useful for Java development, and as a new PHP'er I'd like to use the same concepts JAXB provides in a PHP world.

2
  • JAXB does two things - java code generation from XML Schema, and runtime binding of XML to java objects. Which one you you want?
    – skaffman
    Mar 28, 2010 at 9:25
  • Runtime binding of XML->Object. Mar 28, 2010 at 21:09

5 Answers 5

8

I was also trying to find the same thing before, but couldn't. So I decided to write my own library for PHP 5.3 which mirrors JAXB's annotations to bind objects to XML.

Check it out here: https://github.com/lampjunkie/xml-hitch

Hopefully others will find this useful.

4

I wrote a simple and based on the annotations PAXB: https://github.com/ziollek/PAXB. Check whether this solution is sufficient.

Sample classes with XML binding annotations

/**
 * @XmlElement(name="root")
 */
class SampleEntity {

    /**
     * @XmlElement(name="attribute-value", type="AttributeValueEntity")
     */
    private $nestedEntity;

    private $text;

    /**
     * @XmlElementWrapper(name="number-list")
     */
    private $number = array();


    public function __construct($number = array(), $nestedEntity = null, $text = "")
    {
        $this->number = $number;
        $this->nestedEntity = $nestedEntity;
        $this->text = $text;
    }
}

class AttributeValueEntity {

    /**
     * @XmlAttribute
     */
    private $attribute;

    /**
     * @XmlElement
     */
    private $value;

    /**
     * @param string $attribute
     * @param string $value
     */
    public function __construct($attribute = "", $value = "")
    {
        $this->attribute = $attribute;
        $this->value = $value;
    }

    /**
     * @return string
     */
    public function getAttribute()
    {
        return $this->attribute;
    }

    /**
     * @return string
     */
    public function getValue()
    {
        return $this->value;
    }
}

Marshalling example:

 $sampleEntity = new SampleEntity(
    array(1,2,3),
    new AttributeValueEntity('sample attribure', 'sample value'),
    'Sample text'
);

echo PAXB\Setup::getMarshaller()->marshall($sampleEntity, true);

and the output:

<?xml version="1.0"?>
<root>
    <attribute-value attribute="sample attribure">
        <value>sample value</value>
    </attribute-value>
    <text>Sample text</text>
    <number-list>
        <number>1</number>
        <number>2</number>
        <number>3</number>
    </number-list>
</root>

Unmarshalling

$xmlInput = '...'; //as above
/** @var SampleEntity $sampleEntity */
$sampleEntity = PAXB\Setup::getUnmarshaller()->unmarshall($xmlInput, 'SampleEntity');
0
2

I was looking for something similar to JAXB but for PHP,

PiXB seems similar to JAXB, actually I didn't tried it but looking at the examples seems promising

1

Try https://github.com/moyarada/XSD-to-PHP

0

There is a composer package for it: sabre/xml. You can install it with composer require sabre/xml. There is a homepage for tutorials and examples See http://sabre.io/xml/

It is easy to use and feature rich and actively maintained.

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.