Processing JSON in JavaScript
To process a JSON document in JavaScript, we can use the following code:
// Parse JSON
const jsonString = '{"name": "John", "age": 30, "city": "New York"}';
const jsonData = JSON.parse(jsonString);
// Accessing JSON data
console.log("Name:", jsonData.name);
console.log("Age:", jsonData.age);
console.log("City:", jsonData.city);
// Modifying JSON data
jsonData.age = 31;
const updatedJsonString = JSON.stringify(jsonData);
console.log("Updated JSON:", updatedJsonString);
Here, a JSON string `jsonString` is defined and then parsed into a JavaScript object `jsonData` using `JSON.parse()`. The JSON data is then accessed, modified, and the updated JSON is serialized back to a string using `JSON.stringify()`.
Processing XML in JavaScript
To process an XML document in JavaScript, we can use the following code:
// Assuming you have an XML string or document
const xmlString = '<person><name>John</name><age>30</age><city>New York</city></person>';
const parser = new DOMParser();
const xmlDoc = parser.parseFromString(xmlString, "text/xml");
// Accessing XML data
const name = xmlDoc.querySelector('name').textContent;
const age = xmlDoc.querySelector('age').textContent;
const city = xmlDoc.querySelector('city').textContent;
console.log("Name:", name);
console.log("Age:", age);
console.log("City:", city);
// Modifying XML data
xmlDoc.querySelector('age').textContent = '31';
const updatedXmlString = new XMLSerializer().serializeToString(xmlDoc);
console.log("Updated XML:", updatedXmlString);
Here, an XML string `xmlString` is defined and then parsed into an XML document using `DOMParser()`. The XML data is then accessed, modified, and the updated XML is serialized back to a string using `XMLSerializer()`.
Processing JSON in PHP
To process a JSON document in PHP, we can use the following code:
<?php
// Parse JSON
$jsonString = '{"name": "John", "age": 30, "city": "New York"}';
$jsonData = json_decode($jsonString, true);
// Accessing JSON data
echo "Name: " . $jsonData['name'] . "\n";
echo "Age: " . $jsonData['age'] . "\n";
echo "City: " . $jsonData['city'] . "\n";
// Modifying JSON data
$jsonData['age'] = 31;
$updatedJsonString = json_encode($jsonData);
echo "Updated JSON: " . $updatedJsonString . "\n";
?>
Here, a JSON string `jsonString` is defined and then decoded into an associative array using `json_decode()`. The JSON data is then accessed, modified, and the updated JSON is encoded back to a string using `json_encode()`.
Processing XML in PHP
To process an XML document in PHP, we can use the following code:
<?php
// Assuming you have an XML string or document
$xmlString = '<person><name>John</name><age>30</age><city>New York</city></person>';
$xmlDoc = simplexml_load_string($xmlString);
// Accessing XML data
$name = $xmlDoc->name;
$age = $xmlDoc->age;
$city = $xmlDoc->city;
echo "Name: " . $name . "\n";
echo "Age: " . $age . "\n";
echo "City: " . $city . "\n";
// Modifying XML data
$xmlDoc->age = '31';
$updatedXmlString = $xmlDoc->asXML();
echo "Updated XML: " . $updatedXmlString . "\n";
?>
Here, an XML string `xmlString` is defined and then loaded into a SimpleXMLElement object using `simplexml_load_string()`. The XML data is then accessed, modified, and the updated XML is retrieved as a string using `$xmlDoc->asXML()`.