how to know string contains xml data or not using javascript?

check if string contains html tags javascript
javascript check if(string contains special characters)
javascript string contains character
jquery check if string contains specific word
check if string contains underscore javascript
javascript string indexof
jquery string contains
javascript string contains case insensitive

how to know string contains XML data or not using java script ?

i am using the following code.

if(str.substr(1,5) == '<?xml')
{
alert("xml data");
}else{
alert('not an xml data');
}

The problem is some times my XML (str) data does not contains ?xml ............. tag so this code is not working for me

I'd propose using two possible options here:

  1. This one is to use jquery 1.9.1. You can simply pass your string to it, and it will try to parse it. If done - will return length that is greater than 0. Otherwise - will return 0 or throw exception if string is not well formed XML. Working sample is here: http://jsfiddle.net/fqCNL/

  2. This one is to use javascript DOM Parser. The logic is pretty similar. You should try to parse string with parser. If it's firsChild is not defined and firstChildElement local name is 'parsererror' - then it's not xml or not valid xml. Also exception should be handled if it fails - that also will mean that this is simple string, not a valid xml. Working sample is here: http://jsfiddle.net/MwKcq/ Note, that for cross-browser work of this method (primarily for IE) - you'll have to check for if (window.DOMParser) and if it does not exist - use another part for parsing: xmlDoc=new ActiveXObject("Microsoft.XMLDOM"); xmlDoc.async=false; xmlDoc.loadXML(text);

JavaScript String includes() Method, How do you check if a string does not contain a character in JavaScript? The includes() method determines whether a string contains the characters of a specified string. This method returns true if the string contains the characters, and false if not. Note: The includes() method is case sensitive.

function xmpresent(xml){
 try {
 var Resultss = $.xml2json(data.message);
 return true;
 }
 catch (e) { 
 return false;
}
}

How can I check if a single character appears in a string?, How do you check if a character is present in a string? I use this method (executeJavascript()) to call another Javascript functions passing them some string parameters and work fine! It seems to be that this method couldn't finish to ensemble the function call including my XML string (with especial characters, scaped or not)

You can use indexOf()

if(str.indexOf('<?xml') != -1){
}else{}

Check if a string contains a substring in JavaScript, How do you check if a value is a string in JavaScript? Pre-ES6, the common way to check if a string contains a substring was to use indexOf, which is a string method that return -1 if the string does not contain the substring. If the substring is found, it returns the index of the character that starts the string. Like includes(), the second parameters sets the starting point:

How can I determine if a string contains XML in node.js?, the given substring, it returns -1. The indexOf() method is case-sensitive and accepts two parameters. The first parameter is the substring to search for, and the second optional parameter is the index to start the search from (default index is 0). Because each Customer child node has a unique name, you can use the getElementsByTagName() function to retrieve the data they contain. The resulting variables — Name, Age, and Color — are then used to add data to the table on the page.

HTML DOM contains() Method, You could try loading your string using node-htmlparser npm install htmlparser. Since its parser is forgiving with malformed and partial HTML strings you should  The contains() method is Java method to check if String contains another substring or not. It returns boolean value so it can use directly inside if statements. Syntax of String "Contain" method. public boolean String.contains(CharSequence s) Parameters. s − This is the sequence to search . Return Value. This method returns true only if this

Java String contains() Method, This method returns true if the string contains the characters, and false if not. Note​: The Check if a string includes "world", starting the search at position 12:. In C#, String.Contains() is a string method. This method is used to check whether the substring occurs within a given string or not. Syntax: public bool Contains(string str) Parameter: str: It is the string which is to be checked. Type of this parameter is System.String.

Comments
  • Note : Download jquery.xml2json.js from bvbcode.com/code/sdakv8fj-895625-down
  • but my XML data is like this <note> <to> Tove</to> <from>Jani</from> <heading>Reminder</heading> <body>Don't forget me this weekend!</body> </note>
  • And what is the issue you see with my approach? Did you try this already?
  • yes i tried .my XML data is coming from server ,some times it is not well formed XML that means it does not contain<?xml version="1.0" encoding="UTF-8"?> tag in that case it not working for me ,please provide any other solution.
  • Hmm how exactly are you getting this data? An Ajax call or is it directly on the page also do you see any js errors on your developer tools?