burnstony#1975
burnstony#197511mo ago

xml

wondering best way to process xml?
13 Replies
erquhart
erquhart11mo ago
Can you share a little more about what you're trying to do? There are npm libraries for parsing xml, and you can use npm libraries in your convex functions.
burnstony#1975
burnstony#1975OP11mo ago
I have an rss xml document saved to file storage
erquhart
erquhart11mo ago
What are you trying to do that you need help with
erquhart
erquhart11mo ago
If you're successfully saving the file to storage, you can use a library like this to parse in the browser or in a convex function: https://www.npmjs.com/package/fast-xml-parser
npm
fast-xml-parser
Validate XML, Parse XML, Build XML without C/C++ based libraries. Latest version: 4.3.6, last published: a month ago. Start using fast-xml-parser in your project by running npm i fast-xml-parser. There are 2383 other projects in the npm registry using fast-xml-parser.
burnstony#1975
burnstony#1975OP11mo ago
thanks I am pretty new to typescript/javascript so I am never sure what libraries to use
erquhart
erquhart11mo ago
ah, gotcha
burnstony#1975
burnstony#1975OP11mo ago
MDN Web Docs
Parsing and serializing XML - XML: Extensible Markup Language | MDN
At times, you may need to parse XML content and convert it into a DOM tree, or, conversely, serialize an existing DOM tree into XML. In this article, we'll look at the objects provided by the web platform to make the common tasks of serializing and parsing XML easy.
erquhart
erquhart11mo ago
If that's working, standards are the way to go
burnstony#1975
burnstony#1975OP11mo ago
the doc is type blob how do I convert that to a string
erquhart
erquhart11mo ago
In a convex function or in the browser In the browser, FileReader.readAsText() should work: https://developer.mozilla.org/en-US/docs/Web/API/FileReader/readAsText Hmm actually looks like Blob has a text() method that returns a promise, have you tried that?
burnstony#1975
burnstony#1975OP11mo ago
just figure that out all the promises are confusing coming from a java background
erquhart
erquhart11mo ago
yeah fair got it working?
burnstony#1975
burnstony#1975OP11mo ago
DOMParser is not defined tried using fast-xml-parser and it also gives this error const { XMLParser, XMLBuilder, XMLValidator} = require("fast-xml-parser"); handler: async (ctx, args) => { const rss_blob = await ctx.storage.get(args.storageId); if(rss_blob == null){ console.error("doc empty") return } const rss_text: string = await rss_blob.toString() const parser = new XMLParser(); let jObj = parser.parse(rss_text);
const builder = new XMLBuilder(); const xmlContent = builder.build(jObj); }, }); NEVER MIND, not sure why I was getting that error, think I am making progress again

Did you find this page helpful?