javascript - Parsing a string that contains xml like syntax -


I am looking to parse a string that contains inline elements that can be picked up by javascript. An example.

Looking at the parasymal string given below:

"I have a & lt; noun length =" 10 "/>

Is there any way to parse this string easily and understand that there is an object in the string named "noun" that has a property with a value of 10?

You need to make sure that your string is well-formed XML.In this case, Make sure that the document element is present.

How to wrap a value in a element & gt; and then the paras and query example for the attribute value :

  Function Parsippipit (Snippet, Xpath Expression) {var startDoc = "& lt; Doc & gt; ", endDoc =" & lt; / Doc> ", xml = startDoc + snippet + endok, xmlDoc; if (window. Dompirs) {pars = new dumpparser (); xmlDoc = parser.parseFromString (Xml," text / xml ");} and // Internet Explorer {xmlDoc = New ActiveXObject ("Mic rosoft.XMLDOM"); XmlDoc.async = false; xmlDoc.loadXML (xml);} return xmlDoc;} Var xmlDoc = parseSnippet ('I have a & lt; noun length = " 10 "/>;); var nounLength = document.evaluate ('/ doc / noun / @ length', xmlDoc, null, XPathResult.FIRST_ORDERED_NODE_TYPE, blank) .singleNodeValue.textContent; Warning (" Length: "+ Lang Lang );  

Below is the same concept, but taking advantage of jQuery and functions:

< Pre> function parsecript (snippet) {var startDoc = "& lt; Doc & gt; ", EndDoc =" & lt; / Doc & gt; ", xml = startDoc + snippet + endococ, xmlDoc = $ .parseXML (xml); return $ (xmlDoc);} var $ length = parseSnippet ('I have a & lt; noun length =" 10 " / & Gt; ') .find ("noun") .attr ("length"); Console.log ("length:" + $ length);

Comments

Popular posts from this blog

java - org.apache.http.ProtocolException: Target host is not specified -

java - Gradle dependencies: compile project by relative path -

ruby on rails - Object doesn't support #inspect when used with .include -