Closed ledlamp closed 4 months ago
We're glad you find this project helpful. We'll try to address this issue ASAP. You can vist https://solothought.com to know recent features. Don't forget to star this repo.
What version of the library do you use? Does it work when you don't set textNodeName: "text",
?
parser supports only string.
it is string
since youtube url expire.. try this code on https://npm.runkit.com/fast-xml-parser
var {XMLParser} = require("fast-xml-parser")
var parser = new XMLParser({
ignoreAttributes: false,
textNodeName: "text",
attributeNamePrefix: ""
});
var xml = `
<?xml version="1.0" encoding="utf-8" ?><transcript><text start="0.48" dur="4.04">hello everyone this is your daily dose</text><text start="2.36" dur="4.32">of Internet the police in Peru dressed</text><text start="4.52" dur="4.96">up on Valentine&#39;s Day to get a suspect</text><text start="6.68" dur="2.8">to come out of their</text><text start="12.24" dur="5.879">house she thought she had a secret admir</text><text start="15.16" dur="2.959">but got arrested in</text><text start="23.96" dur="6.639">St I got it this</text><text start="28.279" dur="5.201">time I got it this</text><text start="30.599" dur="2.881">oh</text><text start="37.52" dur="5.48">my this guy put an electric muscle</text><text start="40" dur="3">stimulator on his</text><text start="58.92" dur="3.959">face</text><text start="60.76" dur="6.48">hold okay so listen up I&#39;m not going to</text><text start="62.879" dur="6.801">say this again 6th fifth 4th 3D second 1</text><text start="67.24" dur="4.84">and then reverse all the way over and up</text><text start="69.68" dur="5.2">there&#39;s your clutch break gas now you&#39;re</text><text start="72.08" dur="5.2">ready you got</text><text start="74.88" dur="5.279">this this has to be the weirdest sound</text><text start="77.28" dur="2.879">I&#39;ve ever heard from a</text><text start="88.84" dur="3">choir</text><text start="108.68" dur="3.68">how do you even end up in this type of</text><text start="118.28" dur="9.319">situation want to hug him I wish I could</text><text start="121.759" dur="9.881">yeah and kiss him yeah cuz you love him</text><text start="127.599" dur="4.041">yeah well was your dad&#39;s</text><text start="131.84" dur="4.24">dead bro No One Believes me that my dog</text><text start="134.239" dur="3.801">is scared of Kim Jong-un he does not</text><text start="136.08" dur="4.239">like him who is this</text><text start="138.04" dur="5.839">Johnny who is</text><text start="140.319" dur="6.481">that yeah you tell</text><text start="143.879" dur="7.241">him yeah you tell this baby gorilla</text><text start="146.8" dur="7.4">really wants this toy eat it</text><text start="151.12" dur="5.64">yeah I want that I</text><text start="154.2" dur="4.92">know I found a company that makes really</text><text start="156.76" dur="2.36">trippy</text><text start="163.48" dur="3.679">vases this woman somehow gave birth to a</text><text start="166.2" dur="3.679">giant</text><text start="167.159" dur="6.841">baby here he</text><text start="169.879" dur="6.921">is and oh sorry baby I can barely hold</text><text start="174" dur="6.08">him but anyways he is the last time we</text><text start="176.8" dur="5.439">checked he was 24.4 lb this guy proposed</text><text start="180.08" dur="6.92">to his girlfriend at a waffle house I</text><text start="182.239" dur="4.761">just have one question will you marry</text><text start="190.08" dur="3.64">me I got</text><text start="195.08" dur="5.04">you what&#39; you say I will</text><text start="205.08" dur="4.799">okay that is the end of this video we</text><text start="207.64" dur="6.599">hope you enjoyed and I&#39;ll see you guys</text><text start="209.879" dur="4.36">again very very soon later</text></transcript>`;
console.log(JSON.stringify(parser.parse(xml)));
outputs:
{"?xml":{"version":"1.0","encoding":"utf-8"},"transcript":"[object Object][object Object][object Object][object Object][object Object][object Object][object Object][object Object][object Object][object Object][object Object][object Object][object Object][object Object][object Object][object Object][object Object][object Object][object Object][object Object][object Object][object Object][object Object][object Object][object Object][object Object][object Object][object Object][object Object][object Object][object Object][object Object][object Object][object Object][object Object][object Object][object Object][object Object][object Object][object Object][object Object][object Object][object Object][object Object][object Object][object Object][object Object]"}
also try this, same issue:
var {XMLParser} = require("fast-xml-parser")
var parser = new XMLParser({
ignoreAttributes: false,
textNodeName: "asdf",
attributeNamePrefix: ""
});
var xml = `
<?xml version="1.0" encoding="utf-8" ?><transcript><asdf start="0.48" dur="4.04">hello everyone this is your daily dose</asdf><asdf start="2.36" dur="4.32">of Internet the police in Peru dressed</asdf><asdf start="4.52" dur="4.96">up on Valentine&#39;s Day to get a suspect</asdf><asdf start="6.68" dur="2.8">to come out of their</asdf><asdf start="12.24" dur="5.879">house she thought she had a secret admir</asdf><asdf start="15.16" dur="2.959">but got arrested in</asdf><asdf start="23.96" dur="6.639">St I got it this</asdf><asdf start="28.279" dur="5.201">time I got it this</asdf><asdf start="30.599" dur="2.881">oh</asdf><asdf start="37.52" dur="5.48">my this guy put an electric muscle</asdf><asdf start="40" dur="3">stimulator on his</asdf><asdf start="58.92" dur="3.959">face</asdf><asdf start="60.76" dur="6.48">hold okay so listen up I&#39;m not going to</asdf><asdf start="62.879" dur="6.801">say this again 6th fifth 4th 3D second 1</asdf><asdf start="67.24" dur="4.84">and then reverse all the way over and up</asdf><asdf start="69.68" dur="5.2">there&#39;s your clutch break gas now you&#39;re</asdf><asdf start="72.08" dur="5.2">ready you got</asdf><asdf start="74.88" dur="5.279">this this has to be the weirdest sound</asdf><asdf start="77.28" dur="2.879">I&#39;ve ever heard from a</asdf><asdf start="88.84" dur="3">choir</asdf><asdf start="108.68" dur="3.68">how do you even end up in this type of</asdf><asdf start="118.28" dur="9.319">situation want to hug him I wish I could</asdf><asdf start="121.759" dur="9.881">yeah and kiss him yeah cuz you love him</asdf><asdf start="127.599" dur="4.041">yeah well was your dad&#39;s</asdf><asdf start="131.84" dur="4.24">dead bro No One Believes me that my dog</asdf><asdf start="134.239" dur="3.801">is scared of Kim Jong-un he does not</asdf><asdf start="136.08" dur="4.239">like him who is this</asdf><asdf start="138.04" dur="5.839">Johnny who is</asdf><asdf start="140.319" dur="6.481">that yeah you tell</asdf><asdf start="143.879" dur="7.241">him yeah you tell this baby gorilla</asdf><asdf start="146.8" dur="7.4">really wants this toy eat it</asdf><asdf start="151.12" dur="5.64">yeah I want that I</asdf><asdf start="154.2" dur="4.92">know I found a company that makes really</asdf><asdf start="156.76" dur="2.36">trippy</asdf><asdf start="163.48" dur="3.679">vases this woman somehow gave birth to a</asdf><asdf start="166.2" dur="3.679">giant</asdf><asdf start="167.159" dur="6.841">baby here he</asdf><asdf start="169.879" dur="6.921">is and oh sorry baby I can barely hold</asdf><asdf start="174" dur="6.08">him but anyways he is the last time we</asdf><asdf start="176.8" dur="5.439">checked he was 24.4 lb this guy proposed</asdf><asdf start="180.08" dur="6.92">to his girlfriend at a waffle house I</asdf><asdf start="182.239" dur="4.761">just have one question will you marry</asdf><asdf start="190.08" dur="3.64">me I got</asdf><asdf start="195.08" dur="5.04">you what&#39; you say I will</asdf><asdf start="205.08" dur="4.799">okay that is the end of this video we</asdf><asdf start="207.64" dur="6.599">hope you enjoyed and I&#39;ll see you guys</asdf><asdf start="209.879" dur="4.36">again very very soon later</asdf></transcript>`;
console.log(JSON.stringify(parser.parse(xml)));
the problem is if textNodeName is set to the same as the node name, it breaks...
but I want output like this.
{"?xml":{"version":"1.0","encoding":"utf-8"},"transcript":{"text":[{"text":"hello everyone this is your daily dose","start":"0.48","dur":"4.04"},{"text":"of Internet the police in Peru dressed","start":"2.36","dur":"4.32"},{"text":"up on Valentine's Day to get a suspect","start":"4.52","dur":"4.96"},{"text":"to come out of their","start":"6.68","dur":"2.8"},{"text":"house she thought she had a secret admir","start":"12.24","dur":"5.879"},{"text":"but got arrested in","start":"15.16","dur":"2.959"},{"text":"St I got it this","start":"23.96","dur":"6.639"},{"text":"time I got it this","start":"28.279","dur":"5.201"},{"text":"oh","start":"30.599","dur":"2.881"},{"text":"my this guy put an electric muscle","start":"37.52","dur":"5.48"},{"text":"stimulator on his","start":"40","dur":"3"},{"text":"face","start":"58.92","dur":"3.959"},{"text":"hold okay so listen up I'm not going to","start":"60.76","dur":"6.48"},{"text":"say this again 6th fifth 4th 3D second 1","start":"62.879","dur":"6.801"},{"text":"and then reverse all the way over and up","start":"67.24","dur":"4.84"},{"text":"there's your clutch break gas now you're","start":"69.68","dur":"5.2"},{"text":"ready you got","start":"72.08","dur":"5.2"},{"text":"this this has to be the weirdest sound","start":"74.88","dur":"5.279"},{"text":"I've ever heard from a","start":"77.28","dur":"2.879"},{"text":"choir","start":"88.84","dur":"3"},{"text":"how do you even end up in this type of","start":"108.68","dur":"3.68"},{"text":"situation want to hug him I wish I could","start":"118.28","dur":"9.319"},{"text":"yeah and kiss him yeah cuz you love him","start":"121.759","dur":"9.881"},{"text":"yeah well was your dad's","start":"127.599","dur":"4.041"},{"text":"dead bro No One Believes me that my dog","start":"131.84","dur":"4.24"},{"text":"is scared of Kim Jong-un he does not","start":"134.239","dur":"3.801"},{"text":"like him who is this","start":"136.08","dur":"4.239"},{"text":"Johnny who is","start":"138.04","dur":"5.839"},{"text":"that yeah you tell","start":"140.319","dur":"6.481"},{"text":"him yeah you tell this baby gorilla","start":"143.879","dur":"7.241"},{"text":"really wants this toy eat it","start":"146.8","dur":"7.4"},{"text":"yeah I want that I","start":"151.12","dur":"5.64"},{"text":"know I found a company that makes really","start":"154.2","dur":"4.92"},{"text":"trippy","start":"156.76","dur":"2.36"},{"text":"vases this woman somehow gave birth to a","start":"163.48","dur":"3.679"},{"text":"giant","start":"166.2","dur":"3.679"},{"text":"baby here he","start":"167.159","dur":"6.841"},{"text":"is and oh sorry baby I can barely hold","start":"169.879","dur":"6.921"},{"text":"him but anyways he is the last time we","start":"174","dur":"6.08"},{"text":"checked he was 24.4 lb this guy proposed","start":"176.8","dur":"5.439"},{"text":"to his girlfriend at a waffle house I","start":"180.08","dur":"6.92"},{"text":"just have one question will you marry","start":"182.239","dur":"4.761"},{"text":"me I got","start":"190.08","dur":"3.64"},{"text":"you what' you say I will","start":"195.08","dur":"5.04"},{"text":"okay that is the end of this video we","start":"205.08","dur":"4.799"},{"text":"hope you enjoyed and I'll see you guys","start":"207.64","dur":"6.599"},{"text":"again very very soon later","start":"209.879","dur":"4.36"}]}}
instead, i have to get output like this, with default textNodeName or anything else
{"?xml":{"version":"1.0","encoding":"utf-8"},"transcript":{"text":[{"#text":"hello everyone this is your daily dose","start":"0.48","dur":"4.04"},{"#text":"of Internet the police in Peru dressed","start":"2.36","dur":"4.32"},{"#text":"up on Valentine's Day to get a suspect","start":"4.52","dur":"4.96"},{"#text":"to come out of their","start":"6.68","dur":"2.8"},{"#text":"house she thought she had a secret admir","start":"12.24","dur":"5.879"},{"#text":"but got arrested in","start":"15.16","dur":"2.959"},{"#text":"St I got it this","start":"23.96","dur":"6.639"},{"#text":"time I got it this","start":"28.279","dur":"5.201"},{"#text":"oh","start":"30.599","dur":"2.881"},{"#text":"my this guy put an electric muscle","start":"37.52","dur":"5.48"},{"#text":"stimulator on his","start":"40","dur":"3"},{"#text":"face","start":"58.92","dur":"3.959"},{"#text":"hold okay so listen up I'm not going to","start":"60.76","dur":"6.48"},{"#text":"say this again 6th fifth 4th 3D second 1","start":"62.879","dur":"6.801"},{"#text":"and then reverse all the way over and up","start":"67.24","dur":"4.84"},{"#text":"there's your clutch break gas now you're","start":"69.68","dur":"5.2"},{"#text":"ready you got","start":"72.08","dur":"5.2"},{"#text":"this this has to be the weirdest sound","start":"74.88","dur":"5.279"},{"#text":"I've ever heard from a","start":"77.28","dur":"2.879"},{"#text":"choir","start":"88.84","dur":"3"},{"#text":"how do you even end up in this type of","start":"108.68","dur":"3.68"},{"#text":"situation want to hug him I wish I could","start":"118.28","dur":"9.319"},{"#text":"yeah and kiss him yeah cuz you love him","start":"121.759","dur":"9.881"},{"#text":"yeah well was your dad's","start":"127.599","dur":"4.041"},{"#text":"dead bro No One Believes me that my dog","start":"131.84","dur":"4.24"},{"#text":"is scared of Kim Jong-un he does not","start":"134.239","dur":"3.801"},{"#text":"like him who is this","start":"136.08","dur":"4.239"},{"#text":"Johnny who is","start":"138.04","dur":"5.839"},{"#text":"that yeah you tell","start":"140.319","dur":"6.481"},{"#text":"him yeah you tell this baby gorilla","start":"143.879","dur":"7.241"},{"#text":"really wants this toy eat it","start":"146.8","dur":"7.4"},{"#text":"yeah I want that I","start":"151.12","dur":"5.64"},{"#text":"know I found a company that makes really","start":"154.2","dur":"4.92"},{"#text":"trippy","start":"156.76","dur":"2.36"},{"#text":"vases this woman somehow gave birth to a","start":"163.48","dur":"3.679"},{"#text":"giant","start":"166.2","dur":"3.679"},{"#text":"baby here he","start":"167.159","dur":"6.841"},{"#text":"is and oh sorry baby I can barely hold","start":"169.879","dur":"6.921"},{"#text":"him but anyways he is the last time we","start":"174","dur":"6.08"},{"#text":"checked he was 24.4 lb this guy proposed","start":"176.8","dur":"5.439"},{"#text":"to his girlfriend at a waffle house I","start":"180.08","dur":"6.92"},{"#text":"just have one question will you marry","start":"182.239","dur":"4.761"},{"#text":"me I got","start":"190.08","dur":"3.64"},{"#text":"you what' you say I will","start":"195.08","dur":"5.04"},{"#text":"okay that is the end of this video we","start":"205.08","dur":"4.799"},{"#text":"hope you enjoyed and I'll see you guys","start":"207.64","dur":"6.599"},{"#text":"again very very soon later","start":"209.879","dur":"4.36"}]}}
and then I have to convert this afterwards.
better examples
var {XMLParser} = require("fast-xml-parser")
var parser = new XMLParser({
textNodeName: "text",
});
var xml = `<list><text>foo</text><text>bar</text></list>`;
console.log(parser.parse(xml));
// output: {list: "[object Object][object Object]"}
var parser = new XMLParser({
textNodeName: "ttext",
});
console.log(parser.parse(xml));
// output: {list: {text: ["foo", "bar"]}}
var parser = new XMLParser({
textNodeName: "ttext",
ignoreAttributes: false
});
var xml = `<list><text x="1">foo</text><text x="2">bar</text></list>`;
console.log(parser.parse(xml));
// output: {list: {text: [{ttext: "foo", @_x: "1"}, {ttext: "bar", @_x: "2"}]}}
var parser = new XMLParser({
textNodeName: "text",
ignoreAttributes: false
});
console.log(parser.parse(xml));
// output: {list: "[object Object][object Object]"}
Although textNodeName
is only useful with ignoreAttributes: false
, the issue happens even with attributes ignored.
This happening with version 4.3.2.
Although it can be worked around if the parsed document is of a known structure, arbitrary xml could cause unexpected bug. For example, xml with <#text></#text>
will break with default settings.
var {XMLParser} = require("fast-xml-parser")
var parser = new XMLParser();
var xml = `<list><#text>foo</#text><#text>bar</#text></list>`;
console.log(parser.parse(xml));
output:
{list: "[object Object][object Object]"}
Parser creates temporary nodes to separate text value of tag with with other nested tags. I tried one of your example which has the same name that is used in tag name as well. In such condition, you should choose different name to store tag value. Eg: @text
#text
is not expected as a tag value, Hence, it is used as reserved name to store tag value. If you are expecting tags with this name, you should change the property value
Description
### InputCode
Output
expected data
Would you like to work on this issue?
Bookmark this repository for further updates. Visit SoloThought to know about recent features.