javascript - Why am I getting a ParseError? -
i trying use domparser
method .parsefromstring
convert strings have in array containing html in dom elements.
some of strings getting following parse errors , can't figure out why.
this loop i'm using parse strings , create dom elements (thanks stackoverflow post: converting html string dom elements?)
var x = 0; while (x < stringsarray.length) { var parser = new domparser(); var doc = parser.parsefromstring(stringsarray[x].html, "text/xml"); outputdomelements[x] = doc.firstchild; x++; }
this example of string parsed:
"<div class="instagrampost"><span>siamak amini</span><p>#usa</p><span>posted 1 month ago</span><a href="https://instagram.com/p/3zg3kdgee8/"><img src="https://scontent.cdninstagram.com/hphotos-xaf1/t51.2885-15/s320x320/e15/11377935_1114448771906000_731563461_n.jpg" /></a></div>"
this example of string has parse error:
"<div class="user"> <a href="https://twitter.com/theclarkofben" aria-label="ben clark (screen name: theclarkofben)" data-scribe="element:user_link" target="_blank"> <img alt="" src="https://pbs.twimg.com/profile_images/1877162520/199389_10150123771869463_502259462_6247107_944624_n_normal.jpg" data-src-2x="https://pbs.twimg.com/profile_images/1877162520/199389_10150123771869463_502259462_6247107_944624_n_bigger.jpg" data-scribe="element:avatar"> <span > <span data-scribe="element:name">ben clark</span> </span> <span data-scribe="element:screen_name">@theclarkofben</span> </a> </div><p class="tweet">just testing out twitter feed made. <a href="https://twitter.com/hashtag/halogenpeanut?src=hash" data-scribe="element:hashtag" target="_blank">#halogenpeanut</a> <a href="http://t.co/wtoznysugs" data-pre-embedded="true" data-scribe="" target="_blank">pic.twitter.com/wtoznysugs</a></p><p class="timeposted"><a href="https://twitter.com/theclarkofben/status/611514122509922304">posted on 18 jun</a></p><div class="media"><img src="https://pbs.twimg.com/media/chyi2rqweaajrn-.jpg:large" alt="image tweet" /></div>"
the parse error above string states: error on line 10 @ column 7: opening , ending tag mismatch: img line 0 , a
and here full output .parsefromstring
above string:
<div class="user"><parsererror xmlns="http://www.w3.org/1999/xhtml" style="display: block; white-space: pre; border: 2px solid #c77; padding: 0 1em 0 1em; margin: 1em; background-color: #fdd; color: black"><h3>this page contains following errors:</h3><div style="font-family:monospace;font-size:12px">error on line 10 @ column 7: opening , ending tag mismatch: img line 0 , </div><h3>below rendering of page first error.</h3></parsererror> <a href="https://twitter.com/theclarkofben" aria-label="ben clark (screen name: theclarkofben)" data-scribe="element:user_link" target="_blank"> <img alt="" src="https://pbs.twimg.com/profile_images/1877162520/199389_10150123771869463_502259462_6247107_944624_n_normal.jpg" data-src-2x="https://pbs.twimg.com/profile_images/1877162520/199389_10150123771869463_502259462_6247107_944624_n_bigger.jpg" data-scribe="element:avatar"> <span> <span data-scribe="element:name">ben clark</span> </span> <span data-scribe="element:screen_name">@theclarkofben</span></img></a></div>
is able me identify cause , fix? whitespace in html string perhaps?
you have problems quotings, using double quotes within dom elements variables
var x = "<div class="instagrampost">
.... wrong quotes of html shall single, besides, dom elements shall not have spaces, in order fit in variable...
use doc.body.innerhtml
set parsed html div obtained, not need loop, use "text/html"
, not "text/xml"
when passing content type parser.
below working example.
var html = "<div class='user'><a href='https://twitter.com/theclarkofben' aria-label='ben clark (screen name: theclarkofben)' data-scribe='element:user_link' target='_blank'><img alt='' src='https://pbs.twimg.com/profile_images/1877162520/199389_10150123771869463_502259462_6247107_944624_n_normal.jpg' data-src-2x='https://pbs.twimg.com/profile_images/1877162520/199389_10150123771869463_502259462_6247107_944624_n_bigger.jpg' data-scribe='element:avatar'/><span><span data-scribe='element:name'>ben clark</span></span><span data-scribe='element:screen_name'>@theclarkofben</span></a></div><p class='tweet'>just testing out twitter feed made. <a href='https://twitter.com/hashtag/halogenpeanut?src=hash' data-scribe='element:hashtag' target='_blank'>#halogenpeanut</a> <a href='http://t.co/wtoznysugs' data-pre-embedded='true' data-scribe='' target='_blank'>pic.twitter.com/wtoznysugs</a></p><p class='timeposted'><a href='https://twitter.com/theclarkofben/status/611514122509922304'>posted on 18 jun</a></p><div class='media'><img src='https://pbs.twimg.com/media/chyi2rqweaajrn-.jpg:large'alt='image tweet' /></div>"; parser = new domparser(); doc = parser.parsefromstring(html, "text/html"); document.getelementbyid("parsedhtml").innerhtml = doc.body.innerhtml;
<div id="parsedhtml"></div>
Comments
Post a Comment