Reputation: 623
In Angular 8 I have to send an XML contents to the server.
I prefer converting it to JSON and then it send with:
this.http.post (${BASE_URL}
,body).subscribe
Is it a wise step ?
Generally: How can I read a file in client side and copy its contents into body ?
Thank you in advance
Upvotes: 1
Views: 488
Reputation: 163458
There are many tools and libraries for converting XML to JSON, and they all do it differently. All of them have different strengths and weaknesses; they differ in what kinds of XML they handle well. If the JSON that you want to generate has already been defined by a third party, then you're unlikely to find a tool that generates precisely the desired format. In such cases you're better off writing the conversion rules yourself in XSLT.
Upvotes: 1
Reputation: 28549
jsonxml should what you're looking for.
function xml2json(xml, // element or document DOM node
tab) // tab or indent string for pretty output formatting
// omit or use empty string "" to supress.
// returns JSON string
Upvotes: 0
Reputation: 7949
You have to install "xml2js" dependency.I hope it will helps you.
import { parseString } from "xml2js";
let xml = `<book><title>Some title</title>
<description>some description </description>
<author>
<id>1</id>
<name>some author name</name>
</author>
<review>nice book</review>
<review>this book sucks</review>
<review>amazing work</review></book>
`;
parseString(xml, function(err, result) {
console.dir(result);
});
}
Upvotes: 0