App.jsx
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 |
import React, { useState } from "react"; import { Container, Form, Button, Alert } from "react-bootstrap"; import XMLViewer from "react-xml-viewer"; import "bootstrap/dist/css/bootstrap.min.css"; const App = () => { const [xmlContent, setXmlContent] = useState(""); const [error, setError] = useState(""); const handleFileUpload = (e) => { const file = e.target.files[0]; if (!file) { setError("No file selected."); return; } if (!file.name.endsWith(".xml")) { setError("Please upload a valid XML file."); return; } setError(""); const reader = new FileReader(); reader.onload = (event) => { setXmlContent(event.target.result); }; reader.onerror = () => { setError("Error reading the file."); }; reader.readAsText(file); }; return ( <Container className="my-4"> <h1 className="text-center">React XML Viewer</h1> {/* File Upload */} <Form.Group controlId="xmlFile" className="my-3"> <Form.Label>Upload an XML File</Form.Label> <Form.Control type="file" accept=".xml" onChange={handleFileUpload} /> </Form.Group> {error && <Alert variant="danger">{error}</Alert>} {/* Display XML Viewer */} {xmlContent && ( <div className="mt-4"> <h5>XML Content:</h5> <XMLViewer xml={xmlContent} /> </div> )} </Container> ); }; export default App; |