Skip to main content

How do I write this kind of PHP code in Javascript? [duplicate]

I am trying to convert a JSON file to XML using JavaScript. Until now, I used this library : https://github.com/abdolence/x2js ; It does give me the results, but not in the way I want them.

Here's what my JSON looks like :

{
    "data": {
        "key4":{
            "sample8": [
                {
                    "sample9":"val",
                    "sample10":"val"
                },
                {
                    "sample11":"val",
                    "sample12":"val"
                },
                {
                    "sample13":"val",
                    "sample14":"val"
                }
            ]
        }
    }
}

Here's what the XML I get from the library looks like :

<data>
    <key4>
        <sample8>
            <sample9>val</sample9>
            <sample10>val</sample10>
        </sample8>
        <sample8>
            <sample11>val</sample11>
            <sample12>val</sample12>
        </sample8>
        <sample8>
            <sample13>val</sample13>
            <sample14>val</sample14>
        </sample8>
    </key4>
</data>

What I am trying to do is to add <item> ... </item> as element name for every item in JSON array, so my expected end result looks like -

<data>
    <key4>
        <sample8>
            <item>
                <sample9>val</sample9>
                <sample10>val</sample10>
            </item>
            <item>
                <sample11>val</sample11>
                <sample12>val</sample12>
            </item>
            <item>
                <sample13>val</sample13>
                <sample14>val</sample14>
            </item>
        </sample8>
    </key4>
</data>

The following code (in PHP) works perfectly for me, and it adds the <item> as well. -

function toXml($node, $array)
{
  foreach ($array as $key => $value) {
    if (is_array($value)) {
      toXml($node->addChild(is_numeric($key) ? 'item' : $key), $value);
    } else {
      $node->addChild($key, $value);
    }
  }
}

$json = <<<JSON
... my json
JSON;

$jsonArr = json_decode($json, true);
$xml = new SimpleXMLElement('<data/>');

toXml($xml, array_shift($jsonArr));

echo $xml->asXML();

But I am supposed to do this in JS instead of PHP, so I am pretty confused on what should I do... I have read many posts/topics here & on internet, but none of them show how to do something like this; atleast the ones I read... What could be added so I get my desired result? Thanks in advance! :)

Via Active questions tagged javascript - Stack Overflow https://ift.tt/2FdjaAW

Comments

Popular posts from this blog

Confusion between commands.Bot and discord.Client | Which one should I use?

Whenever you look at YouTube tutorials or code from this website there is a real variation. Some developers use client = discord.Client(intents=intents) while the others use bot = commands.Bot(command_prefix="something", intents=intents) . Now I know slightly about the difference but I get errors from different places from my code when I use either of them and its confusing. Especially since there has a few changes over the years in discord.py it is hard to find the real difference. I tried sticking to discord.Client then I found that there are more features in commands.Bot . Then I found errors when using commands.Bot . An example of this is: When I try to use commands.Bot client = commands.Bot(command_prefix=">",intents=intents) async def load(): for filename in os.listdir("./Cogs"): if filename.endswith(".py"): client.load_extension(f"Cogs.{filename[:-3]}") The above doesnt giveany response from my Cogs ...

How to show number of registered users in Laravel based on usertype?

i'm trying to display data from the database in the admin dashboard i used this: <?php use Illuminate\Support\Facades\DB; $users = DB::table('users')->count(); echo $users; ?> and i have successfully get the correct data from the database but what if i want to display a specific data for example in this user table there is "usertype" that specify if the user is normal user or admin i want to user the same code above but to display a specific usertype i tried this: <?php use Illuminate\Support\Facades\DB; $users = DB::table('users')->count()->WHERE usertype =admin; echo $users; ?> but it didn't work, what am i doing wrong? source https://stackoverflow.com/questions/68199726/how-to-show-number-of-registered-users-in-laravel-based-on-usertype

Where and how is this Laravel kernel constructor called? [closed]

Where and how is this Laravel kernel constructor called? public fucntion __construct(Application $app, $Router $roouter) { } I have read the documentation and some online tutorial but I can find any clear explanation. I am learning Laravel and I am wondering where does this kernel constructor receives its arguments from. "POSTMOTERM" CLARIFICATION: Here is more clarity.I have checked the boostrap/app.php and it is only used for boostrapping the interfaces into the container class. What is not clear to me is where and how the Kernel class is instatiated and the arguments passed to the object calling the constructor.Something similar to; obj = new kernel(arg1,arg2) or, is the framework using some magic functions somewhere? Special gratitude to those who burn their eyeballs and brain cells on this trivia before it goes into a full blown menopause alias "MARKED AS DUPLICATE". To some of the itchy-finger keyboard warriors, a.k.a The mods,because I believe in th...