Save data from a form in an XML with php

0

How could I save data from a form to an XML? The structure of my XML is as follows:

<assessmentItems>

    <assessmentItem complejidad="3" tema="micologia">

        <itemBody> 

            <p>Que Amanita es comestible?</p>

        </itemBody>

        <correctResponse>

            <value>Caesarea</value>

        </correctResponse>

        <incorrectResponses>

            <value>Phalloides</value>

            <value>Muscaria</value>

            <value>Virosa</value>

        </incorrectResponses>

    </assessmentItem>
    </assessmentItems>

My form is as follows:

            <label for="pregunta"><strong>Pregunta(*):</strong></label>
            <input type="text" name="pregunta" minlength="10" id="galdera" required/><br>

            <label for="correcta"><strong>Respuesta correta (*):</strong></label>
            <input type="text" name="correcta" id="respuestaCorrecta" required/><br>

            <label for="errada1"><strong>Respuesta errada 1 (*):</strong></label>
            <input type="text" name="errada1" id="respuestaErrada1" required/><br>
            <label for="errada2"><strong>Respuesta errada 2 (*):</strong></label>
            <input type="text" name="errada2" id="respuestaErrada2" required/><br>

......................................... and here I would follow more form ....

    
asked by UnaiLopez 05.11.2017 в 13:10
source

1 answer

0

Since the version> 5.1.3 of PHP, new methods were introduced that give us the ability to modify the XML document.

These methods are:

SimpleXMLElement::addChild();
SimpleXMLElement::addAttribute();

I recommend reading more about it in the official PHP doc:
link

With the first method we can add a new child element.
It accepts two parameters:
1-Name of the attribute
2-Attribute value

With the second method we add an attribute.
It accepts 3 parameters.
1-Indicates the name of the new element
2 (optional) - indicates the value of the element
3 (optional) - indicates the namespace to which the element belongs to

EXAMPLE:
'

<assessmentItem complejidad="3" tema="micologia">

    <itemBody> 

        <p>Que Amanita es comestible?</p>

    </itemBody>

    <correctResponse>

        <value>Caesarea</value>

    </correctResponse>

    <incorrectResponses>

        <value>Phalloides</value>

        <value>Muscaria</value>

        <value>Virosa</value>

    </incorrectResponses>

</assessmentItem>

Primero cargamos el XML y lo transformamos en objeto <br> $ asmentitems = new SimpleXMLElement ('asessmentitem.xml', null, true); '

**** Now let's add an item
* We create an item and add it to the root element of assmentitems $item = $asmentitems->addChild('assessmentItem');

* Here we assign the attribute of the new created item; $item->addAttribute('complejidad','4');

* We create the structure within the new item $item->addChild('itemBody', 'Que Amanita NO es comestible?'); $item->addChild('correctResponse','Pierita'); $item->addChild('incorrectResponses','Aluminio'); $item... To visualize how the XML is, we can show them in the browser by sending a header indicating to the browser that the content to be displayed is not simple text, not HTML, but XML.

This is done with: header('content-type:text/xml'); echo $asmentitems->asXML();

To save the changes of the XML file is:
$asmentitems->asXML('nuevos-asmentitems.xml');

I hope it helps you. Greetings!

PS: I must add that the code I have made directly here, so it is "pseudocodigo", you may need to adapt some parameter.
As an added tip, if you have the possibility, I would tell you to modify the name of each element by something similar to "amItem" or something visually more comfortable when making your modification logic, since the difference between the set (assessmentItems) and each item (assessmentItem) is very similar and can be confusing if you are playing a group or an element, you get a letter: P

Remember to value positively if a response is correct, helping other users find this solution.

    
answered by 05.11.2017 / 19:04
source