1

I want to make an xml file from php like this.

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<feed>
    Everything from php form will go here like this:
    <item sdImg="" hdImg="">
        <title></title>
        <contentId></contentId>
        <contentType></contentType>
        <contentQuality></contentQuality>
        <streamFormat></streamFormat>
        <media>
            <streamQuality></streamQuality>
            <streamBitrate></streamBitrate>
            <streamUrl></streamUrl>
        </media>
        <synopsis></synopsis>
        <genres></genres>
        <runtime></runtime>
    </item>
    now this elements will be submitted again and again with different values.
</feed>

Now the problem is that I want the output of form to be written between this Grandparent feed tag. I thought FOPEN and FWRITE will be useful but I don't think that they can be used here. So what could be the most useful function of php which could be used here. And also the form will going to write in the same xml file again and again. I mean that every time the user will submit the form.

Thanks in advance for your answer.

2 Answers 2

1

You can generate the xml object using something like DOMDocument, and save the output as an XML file.

Sign up to request clarification or add additional context in comments.

Comments

1

Since $_POST is an array, you can convert the post data to an XML format using PHP's SimpleXML.

$xml = new SimpleXMLElement('<feed/>');
array_walk_recursive( $_POST, array( $xml, 'addChild' ) );
print $xml->asXML( );

You may need to array_flip the content to get the key/value pairs to match according to your use.

Note that you may want to scrub the posted data to ensure that it is safe for whatever you are using it for.

1 Comment

Sorry I just messed up with my question I want every output tp writtent between these tags like <item>some value from $_POST here</item> but all this data should be written in this <feed></feed> tags. I have edited my question check it now.

Your Answer

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