XML to JSON Converter

 <!DOCTYPE html>

<html lang="en">

<head>

    <meta charset="UTF-8">

    <meta name="viewport" content="width=device-width, initial-scale=1.0">

    <title>XML to JSON Converter</title>

    <link rel="stylesheet" href="styles.css">

    <script defer src="https://cdnjs.cloudflare.com/ajax/libs/xml2json/0.11.0/xml2json.min.js"></script>

    <script defer src="script.js"></script>

</head>

<body>

    <div class="container">

        <h1>XML to JSON Converter</h1>

        <textarea id="xmlInput" placeholder="Paste your XML here..."></textarea>

        <div class="buttons">

            <button onclick="convertXMLtoJSON()">Convert</button>

            <button onclick="clearFields()">Clear</button>

            <button id="copyBtn">📋 Copy</button>

        </div>

        <pre id="jsonOutput" class="output"></pre>

        <p id="errorMsg"></p>

    </div>

</body>

</html>

/* Google Font */
@import url('https://fonts.googleapis.com/css2?family=Poppins:wght@300;400;600&display=swap');

* {
    margin: 0;
    padding: 0;
    box-sizing: border-box;
    font-family: 'Poppins', sans-serif;
}

body {
    background: linear-gradient(135deg, #8E44AD, #3498DB);
    color: white;
    display: flex;
    justify-content: center;
    align-items: center;
    height: 100vh;
    text-align: center;
    padding: 20px;
}

.container {
    background: rgba(255, 255, 255, 0.2);
    padding: 20px;
    border-radius: 12px;
    box-shadow: 0 4px 10px rgba(0, 0, 0, 0.2);
    max-width: 600px;
    width: 100%;
}

h1 {
    margin-bottom: 20px;
    font-size: 24px;
}

textarea {
    width: 100%;
    height: 150px;
    padding: 10px;
    border-radius: 5px;
    border: none;
    outline: none;
    font-size: 16px;
    background: #2C3E50;
    color: #ecf0f1;
    resize: none;
}

.buttons {
    display: flex;
    justify-content: space-between;
    margin: 15px 0;
}

button {
    padding: 10px;
    border: none;
    border-radius: 5px;
    font-size: 16px;
    cursor: pointer;
    transition: 0.3s;
    flex: 1;
    margin: 0 5px;
}

button:hover {
    opacity: 0.8;
}

/* Button Colors */
button:nth-child(1) {
    background: #3498db;
    color: white;
}

button:nth-child(2) {
    background: #e74c3c;
    color: white;
}

#copyBtn {
    background: #2ecc71;
    color: white;
}

.output {
    background: #1A1A1A;
    padding: 10px;
    border-radius: 5px;
    white-space: pre-wrap;
    text-align: left;
    color: #ffffff;
    font-size: 14px;
    max-height: 300px;
    overflow-y: auto;
}

/* JSON Syntax Highlighting */
.key { color: #f1c40f; }
.string { color: #2ecc71; }
.number { color: #e67e22; }
.boolean { color: #9b59b6; }
.null { color: #e74c3c; }

#errorMsg {
    color: yellow;
    font-size: 14px;
    margin-top: 10px;
}
document.getElementById('copyBtn').addEventListener('click', copyToClipboard);

function convertXMLtoJSON() {
    let xmlInput = document.getElementById('xmlInput').value.trim();
    let jsonOutput = document.getElementById('jsonOutput');
    let errorMsg = document.getElementById('errorMsg');

    if (!xmlInput) {
        errorMsg.textContent = "Please enter valid XML!";
        jsonOutput.textContent = "";
        return;
    }

    try {
        let xmlDoc = new DOMParser().parseFromString(xmlInput, "application/xml");
        let parserError = xmlDoc.getElementsByTagName("parsererror");

        if (parserError.length > 0) {
            throw new Error("Invalid XML format!");
        }

        let json = xml2json(xmlDoc, { compact: true, spaces: 4 });
        jsonOutput.innerHTML = syntaxHighlight(json);
        errorMsg.textContent = "";
    } catch (error) {
        errorMsg.textContent = "Invalid XML format!";
        jsonOutput.textContent = "";
    }
}

function clearFields() {
    document.getElementById('xmlInput').value = "";
    document.getElementById('jsonOutput').textContent = "";
    document.getElementById('errorMsg').textContent = "";
}

function copyToClipboard() {
    let jsonOutput = document.getElementById('jsonOutput').innerText;

    if (jsonOutput === '') {
        alert("Nothing to copy!");
        return;
    }

    navigator.clipboard.writeText(jsonOutput).then(() => {
        alert("Copied to Clipboard!");
    });
}

/* Syntax Highlighting */
function syntaxHighlight(json) {
    json = json.replace(/&/g, "&amp;").replace(/</g, "&lt;").replace(/>/g, "&gt;");
    return json.replace(/("(\\u[a-zA-Z0-9]{4}|\\[^u]|[^"\\])*")|(\b(true|false|null)\b)|(\b-?\d+(\.\d+)?([eE][+-]?\d+)?\b)/g, function(match) {
        let cls = "number";
        if (/^"/.test(match)) {
            cls = /:$/.test(match) ? "key" : "string";
        } else if (/true|false/.test(match)) {
            cls = "boolean";
        } else if (/null/.test(match)) {
            cls = "null";
        }
        return `<span class="${cls}">${match}</span>`;
    });
}

Contact Form