0
<root>
<item0><courseId>133</courseId></item0>
<item1><question>What is php?</question></item1>
<item2><ansIndex>1</ansIndex></item2>
<item3><ans>Sever Side Lang.</ans></item3>
<item4><ans>Web Lang.</ans></item4>
<item5><ans>NOne</ans></item5>
<item6><ans>Both</ans></item6>
</root>

My Xml Function Give This Type Of Output..Bt i Want

<root>
<courseId>133</courseId>
<question>What is php?</question>
<ansIndex>1</ansIndex>
<ans>Sever Side Lang.</ans>
<ans>Web Lang.</ans>
<ans>NOne</ans>
<ans>Both</ans>
</root>



This is my Xml Function ..
$array = json_decode($_GET['data'],true);
function array_to_xml($array, &$xml_user_info) {
    foreach($array as $key => $value) {
        if(is_array($value)) {
            if(!is_numeric($key)){
                $subnode = $xml_user_info->addChild("$key");
                array_to_xml($value, $subnode);
            }else{
                $subnode = $xml_user_info->addChild("item$key");
                array_to_xml($value, $subnode);
            }
        }else {
            $xml_user_info->addChild("$key",htmlspecialchars("$value"));
        }
    }
}

$xml_user_info = new SimpleXMLElement("<?xml version=\"1.0\"?><root></root>");
array_to_xml($array,$xml_user_info);
$xml_file = $xml_user_info->asXML('users.xml');
if($xml_file){
    echo 'XML file have been generated successfully.';
}else{
    echo 'XML file generation error.';
}

N also I Want Set Attribute In ans Tag..
example ...if ansIndex is 1 So i want set one attribute in first ans tag 'right'.

Edited by Vivek_13

3
Contributors
7
Replies
32
Views
2 Years
Discussion Span
Last Post by Vivek_13
0

yes sir ...Privious pbm was Solved ...bt now i confused ..how to Set attribute in Specific tag...

0

Actually your code working fine, the problem is mainly because your $_GET['data']. I had tested the code using the json string you provided in the thread @diafol mentioned and it is working fine: $array = json_decode('{"courseId":"116","question":"asdasd","ansIndex":"1","ans0":"sadsa","ans1":"dsad","ans2":"sadasd","ans3":"asdasd"}',true);
test.jpg

0

Thxs Sir.....now Suppose ansIndex Is 1 so i want to add attribute in ans0 Tag...if 2 So ans 1...

1

Here is an example I tried and working, you can refer and modify accordingly to suit your copy:

<?php
if(!isset($_POST['courseId'])){
?>
<script src = "http://code.jquery.com/jquery-1.11.3.min.js"></script>
<script>
    $(document).ready(function() {
        var arr = {
            "courseId": "144",
            "question": "What is php?",
            "ansIndex": '1',
            "ans": []
        };
        arr['ans'].push("Sever Side Lang.");
        arr['ans'].push("Web Lang.");
        arr['ans'].push("NOne");
        arr['ans'].push("Both");
        console.log(arr);
        $.ajax({
            url: 'test.php',
            type:'POST',
            data: arr,
            success: function(res) { alert(res); } 
        });
    });
</script>

<?php
}else{
    function array_to_xml($array, &$xml_user_info) {
        foreach($array as $key => $value) {
            if(is_array($value)) {
                foreach($value as $ans){
                    $xml_user_info->addChild("$key",htmlspecialchars("$ans"));
                }
            }else {
                $xml_user_info->addChild("$key",htmlspecialchars("$value"));
            }
        }
    }
    $xml_user_info = new SimpleXMLElement("<?xml version=\"1.0\"?><root></root>");

    //$array = json_decode($_GET['data'],true);
    array_to_xml($_POST,$xml_user_info);
    $xml_file = $xml_user_info->asXML('users.xml');
    if($xml_file){
        echo 'XML file have been generated successfully.';
    }else{
        echo 'XML file generation error.';
    }
}
?>

I changed a bit of the data structure from the ajax and the processes to change the data into xml. Since I doing the whoel part in my 'test.php' so, my code will be having some unneccessary codes for your condition.

And, I more recommend the post mothod than a get method for the ajax.

Edited by lps

0

Sorry to cut across:

yes sir ...Privious pbm was Solved ...bt now i confused ..how to Set attribute in Specific tag...

Your previous thread was not marked solved and you did not reply to our posts. So it was assumed that the thread was still current. To my mind, this is part of the same issue as I asked you to show the structure of the XML that you required. The reasoning behind that was so that we could direct you with regard to attributes, child nodes, etc.

So if your previous thread is indeed solved, please mark it so, but the tthread as it stands does seem a little pointless now.

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.