V
V
Vadim2015-12-21 15:48:39
PHP
Vadim, 2015-12-21 15:48:39

How to generate object from XML?

Is there a library that allows you to do this? I looked at jsm/serializer but it's a bit different.
For example, there are interfaces

interface Form{
  public function addElement(Element $element);
}
interface Element{
  
}
interface Text extends Element{
  public function setName($name);
}

interface Button extends Element{
  public function setLabel($label);
}

I want something from this xml:
<form>
<text name="tf1"/>
<button label="Do it!"/>
</form>

an object of type came out:
$form = new Form();

$t = new Text();
$t->setName('tf1');
$form->addElement($t);

$t = new Button();
$t->setLabel('Do it!');
$form->addElement($t);

Of course, you can write it yourself, but perhaps there is a ready-made library that does this? Of the possibilities, I would like the ability to set aliases, that is, to describe the textbox tag and the Text class was created, to indicate which method is used to add childs (in the example, addElement).

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Shirshov Alexander, 2015-12-22
@Keanor

We implemented such a thing with a separate form factory. Through SimpleXML it's not difficult.
Do you have zf2? If you want a simpler option, then you can read xml through \Zend\Config\Reader\Xml and feed it to the default factory, but then xml will have to be adjusted to the specification.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question