Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- XML:
- <?xml version="1.0" encoding="UTF-8"?>
- <users>
- <user>
- <id>1</id>
- <name>John Doe</name>
- <email>john.doe@example.com</email>
- <age>28</age>
- </user>
- <user>
- <id>2</id>
- <name>Jane Smith</name>
- <email>jane.smith@example.com</email>
- <age>32</age>
- </user>
- <user>
- <id>3</id>
- <name>Emily Johnson</name>
- <email>emily.johnson@example.com</email>
- <age>24</age>
- </user>
- <user>
- <id>4</id>
- <name>Michael Brown</name>
- <email>michael.brown@example.com</email>
- <age>45</age>
- </user>
- <user>
- <id>5</id>
- <name>Linda Davis</name>
- <email>linda.davis@example.com</email>
- <age>37</age>
- </user>
- <user>
- <id>6</id>
- <name>James Wilson</name>
- <email>james.wilson@example.com</email>
- <age>29</age>
- </user>
- <user>
- <id>7</id>
- <name>Patricia Garcia</name>
- <email>patricia.garcia@example.com</email>
- <age>36</age>
- </user>
- <user>
- <id>8</id>
- <name>Robert Martinez</name>
- <email>robert.martinez@example.com</email>
- <age>41</age>
- </user>
- <user>
- <id>9</id>
- <name>Jennifer Lopez</name>
- <email>jennifer.lopez@example.com</email>
- <age>30</age>
- </user>
- <user>
- <id>10</id>
- <name>William Taylor</name>
- <email>william.taylor@example.com</email>
- <age>50</age>
- </user>
- </users>
- JAVA: (Either write this of the java-script that is below this code)
- import org.w3c.dom.*;
- import javax.xml.parsers.*;
- import java.io.*;
- import java.util.Scanner;
- public class UserDetailFetcher {
- public static void main(String[] args) {
- Scanner scanner = new Scanner(System.in);
- System.out.print("Enter User ID: ");
- String userId = scanner.nextLine();
- try {
- File inputFile = new File("users.xml");
- DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
- DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
- Document doc = dBuilder.parse(inputFile);
- doc.getDocumentElement().normalize();
- NodeList nList = doc.getElementsByTagName("user");
- boolean userFound = false;
- for (int i = 0; i < nList.getLength(); i++) {
- Node node = nList.item(i);
- if (node.getNodeType() == Node.ELEMENT_NODE) {
- Element element = (Element) node;
- String id = element.getElementsByTagName("id").item(0).getTextContent();
- if (id.equals(userId)) {
- String name = element.getElementsByTagName("name").item(0).getTextContent();
- String email = element.getElementsByTagName("email").item(0).getTextContent();
- String age = element.getElementsByTagName("age").item(0).getTextContent();
- System.out.println("User Found:");
- System.out.println("ID: " + id);
- System.out.println("Name: " + name);
- System.out.println("Email: " + email);
- System.out.println("Age: " + age);
- userFound = true;
- break;
- }
- }
- }
- if (!userFound) {
- System.out.println("User with ID " + userId + " not found.");
- }
- } catch (Exception e) {
- e.printStackTrace();
- } finally {
- scanner.close();
- }
- }
- }
- JAVASCRIPT: (Either write this of the java that is above this code)
- <!DOCTYPE html>
- <html lang="en">
- <head>
- <meta charset="UTF-8">
- <meta name="viewport" content="width=device-width, initial-scale=1.0">
- <title>User Detail Fetcher</title>
- </head>
- <body>
- <h1>User Detail Fetcher</h1>
- <label for="userId">Enter User ID:</label>
- <input type="text" id="userId" />
- <button id="fetchBtn">Fetch User Details</button>
- <pre id="result"></pre>
- <script>
- document.getElementById('fetchBtn').addEventListener('click', function() {
- const userId = document.getElementById('userId').value;
- fetchUserDetails(userId);
- });
- async function fetchUserDetails(userId) {
- try {
- const response = await fetch('users.xml'); // Adjust path if necessary
- if (!response.ok) {
- throw new Error(`Error: ${response.status} - ${response.statusText}`);
- }
- const text = await response.text();
- const parser = new DOMParser();
- const xmlDoc = parser.parseFromString(text, 'text/xml');
- const users = xmlDoc.getElementsByTagName('user');
- let userFound = false;
- let resultText = '';
- for (let i = 0; i < users.length; i++) {
- const id = users[i].getElementsByTagName('id')[0].textContent;
- if (id === userId) {
- const name = users[i].getElementsByTagName('name')[0].textContent;
- const email = users[i].getElementsByTagName('email')[0].textContent;
- const age = users[i].getElementsByTagName('age')[0].textContent;
- resultText = `User Found:\nID: ${id}\nName: ${name}\nEmail: ${email}\nAge: ${age}`;
- userFound = true;
- break;
- }
- }
- if (!userFound) {
- resultText = `User with ID ${userId} not found.`;
- }
- document.getElementById('result').textContent = resultText;
- } catch (error) {
- console.error('Error fetching data:', error);
- document.getElementById('result').textContent = 'Error fetching data.';
- }
- }
- </script>
- </body>
- </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement