Last active
March 14, 2023 10:14
-
-
Save luismendes070/9159064d4e6ccc6b9bb2c3ceacde2b95 to your computer and use it in GitHub Desktop.
TypesScript programming language XML file reader XML file writer feature #ChatGPT
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import * as fs from 'fs'; | |
import * as xml2js from 'xml2js'; | |
// Read the XML file | |
const xml = fs.readFileSync('file.xml', 'utf-8'); | |
// Convert the XML to a JavaScript object | |
xml2js.parseString(xml, (err, result) => { | |
if (err) { | |
console.error(err); | |
return; | |
} | |
// Do something with the result object | |
console.log(result); | |
}); |
Author
luismendes070
commented
Mar 13, 2023
import * as fs from 'fs';
import * as xmlbuilder from 'xmlbuilder2';
// Create the XML document
const xml = xmlbuilder.create('root')
.ele('child')
.att('foo', 'bar')
.txt('Hello, World!')
.end({ prettyPrint: true });
// Write the XML document to a file
fs.writeFileSync('file.xml', xml);
Conclusion March 14th 2022
[Angular + XML W3Schools Editor URL](https://stackblitz.com/edit/angular-kezwuw?file=src/index.html)
[Angular + XML W3Schools Application URL](https://angular-kezwuw.stackblitz.io)
[DomParser MDN Error Input Validation](https://developer.mozilla.org/en-US/docs/Web/API/DOMParser/parseFromString)
export interface Book {
title: string;
author: string;
year: number;
}
import { Injectable } from '@angular/core';
import { HttpClient } from '@angular/common/http';
import { Book } from './book';
@Injectable({
providedIn: 'root'
})
export class BookService {
private xmlUrl = 'assets/books.xml';
constructor(private http: HttpClient) { }
getBooks(): Promise<Book[]> {
return this.http.get(this.xmlUrl, { responseType: 'text' })
.toPromise()
.then(xml => {
const parser = new DOMParser();
const xmlDoc = parser.parseFromString(xml, 'text/xml');
const books = Array.from(xmlDoc.getElementsByTagName('book'))
.map(bookNode => ({
title: bookNode.getElementsByTagName('title')[0].textContent,
author: bookNode.getElementsByTagName('author')[0].textContent,
year: parseInt(bookNode.getElementsByTagName('year')[0].textContent, 10)
}));
return books;
});
}
addBook(book: Book): Promise<void> {
// TODO: Implement add book logic
return Promise.resolve();
}
updateBook(book: Book): Promise<void> {
// TODO: Implement update book logic
return Promise.resolve();
}
deleteBook(title: string): Promise<void> {
// TODO: Implement delete book logic
return Promise.resolve();
}
}
import { Component, OnInit } from '@angular/core';
import { Book } from '../book';
import { BookService } from '../book.service';
@Component({
selector: 'app-book-list',
templateUrl: './book-list.component.html',
styleUrls: ['./book-list.component.css']
})
export class BookListComponent implements OnInit {
books: Book[];
constructor(private bookService: BookService) { }
ngOnInit() {
this.bookService.getBooks()
.then(books => this.books = books);
}
addBook() {
// TODO: Implement add book logic
}
updateBook(book: Book) {
// TODO: Implement update book logic
}
deleteBook(title: string) {
// TODO: Implement delete book logic
}
}
<h2>Books</h2>
<ul>
<li *ngFor="let book of books">
{{ book.title }} by {{ book.author }} ({{ book.year }})
</li>
</ul>
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment