Solved

How to search XML to find rows containing a value

Posted on 2011-02-20
10
258 Views
Last Modified: 2012-05-11
Hi all..

I have the following xml

<criteria><row><operation>EQUALS</operation><reportkey>C5EFEBE6-82E6-2365-7F11-854FF053CABC</reportkey><seqno>0</seqno><name>V_REPORTING.EXPIRY_DATE</name><value>Ask User At Runtime</value><code></code><operator></operator></row>
</criteria>

I need to find all <row>'s that contain <value>Ask User At Runtime</value>

Any help is greatly appricated
0
Comment
Question by:robert_jenkin
  • 4
  • 4
  • 2
10 Comments
 
LVL 82

Expert Comment

by:leakim971
ID: 34937683
With jQuery : $("value:contains('Ask User At Runtime')", $(xml)).parent("row")

http://api.jquery.com/contains-selector/
http://api.jquery.com/parent/

Test page and detail :


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
<script language="javascript" src="http://code.jquery.com/jquery-1.5.js"></script>
<script language="javascript">
	var xml = "<criteria>";
	xml += "<row><operation>EQUALS0</operation><reportkey>C5EFEBE6-82E6-2365-7F11-854FF053CABC</reportkey><seqno>0</seqno><name>V_REPORTING.EXPIRY_DATE</name><value>Ask User0 At Runtime</value><code></code><operator></operator></row>";
	xml += "<row><operation>EQUALS</operation><reportkey>C5EFEBE6-82E6-2365-7F11-854FF053CABC</reportkey><seqno>0</seqno><name>V_REPORTING.EXPIRY_DATE</name><value>Ask User At Runtime</value><code></code><operator></operator></row>";
	xml += "<row><operation>EQUALS1</operation><reportkey>C5EFEBE6-82E6-2365-7F11-854FF053CABC</reportkey><seqno>0</seqno><name>V_REPORTING.EXPIRY_DATE</name><value>Ask User1 At Runtime</value><code></code><operator></operator></row>";
	xml += "</criteria>";
	$(document).ready(function() {
		var s = "Ask User At Runtime";
		var rightValueNode = $("value:contains(" + s + ")", $(xml));
		var row = rightValueNode.parent("row");
		//checking, we alert the operation node value, the right one is EQUALS
		alert( $("operation", row).text() );
	});
</script>
</head>
<body>
</body>
</html>

Open in new window

0
 
LVL 82

Expert Comment

by:leakim971
ID: 34937750
Simple javascript :

1 - We use getElementsByTagName("value") which return an array
2 - We do a loop over this array to compare value of each node and the value we're searching
3 - Once we found the right node, we take its parent : the row we're looking for
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
<script language="javascript">
	var xml = "<criteria>";
	xml += "<row><operation>EQUALS0</operation><reportkey>C5EFEBE6-82E6-2365-7F11-854FF053CABC</reportkey><seqno>0</seqno><name>V_REPORTING.EXPIRY_DATE</name><value>Ask User0 At Runtime</value><code></code><operator></operator></row>";
	xml += "<row><operation>EQUALS</operation><reportkey>C5EFEBE6-82E6-2365-7F11-854FF053CABC</reportkey><seqno>0</seqno><name>V_REPORTING.EXPIRY_DATE</name><value>Ask User At Runtime</value><code></code><operator></operator></row>";
	xml += "<row><operation>EQUALS1</operation><reportkey>C5EFEBE6-82E6-2365-7F11-854FF053CABC</reportkey><seqno>0</seqno><name>V_REPORTING.EXPIRY_DATE</name><value>Ask User1 At Runtime</value><code></code><operator></operator></row>";
	xml += "</criteria>";
	
	window.onload = function() {

		if(window.DOMParser) xmlDoc = (new DOMParser()).parseFromString(xml,"text/xml");else { xmlDoc = new ActiveXObject("Microsoft.XMLDOM");xmlDoc.async = "false";xmlDoc.loadXML(xml); }				
		var s = "Ask User At Runtime";
		var _values = xmlDoc.getElementsByTagName("value");
		var rightValue = null;
		for(var i=0;i<_values.length;i++) {
			if(_values[i].childNodes[0].nodeValue == s) {
				rightValue = _values[i];
				break;
			}
		}
		var row = rightValue.parentNode;
		//checking, we alert the operation node value, the right one is EQUALS
		alert( row.getElementsByTagName("operation")[0].childNodes[0].nodeValue );
	};
	
</script>
</head>
<body>
</body>
</html>

Open in new window

0
 
LVL 20

Expert Comment

by:Proculopsis
ID: 34937904

You could try something like this:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8"/>
<title>http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/Q__26834524.html</title>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4/jquery.min.js"></script>
<script type="text/javascript"> 

var xmlSource = '<?xml version="1.0" encoding="utf-8"?>\
<criteria>\
  <row>\
    <operation>EQUALS</operation>\
    <reportkey>C5EFEBE6-82E6-2365-7F11-854FF053CABC</reportkey>\
    <seqno>0</seqno>\
    <name>V_REPORTING.EXPIRY_DATE</name>\
    <value>Ask User At Runtime</value>\
    <code></code>\
    <operator></operator>\
  </row>\
  <row>\
    <operation>EQUALS</operation>\
    <reportkey>C5EFEBE6-82E6-2365-7F11-854FF053CABC</reportkey>\
    <seqno>1</seqno>\
    <name>V_REPORTING.EXPIRY_DATE</name>\
    <value>0</value>\
    <code></code>\
    <operator></operator>\
  </row>\
  <row>\
    <operation>EQUALS</operation>\
    <reportkey>C5EFEBE6-82E6-2365-7F11-854FF053CABC</reportkey>\
    <seqno>2</seqno>\
    <name>V_REPORTING.EXPIRY_DATE</name>\
    <value>Ask User At Runtime</value>\
    <code></code>\
    <operator></operator>\
  </row>\
</criteria>\
';

function parser( xml )
{
  var result = new ActiveXObject( "Microsoft.XMLDOM" );
  result.async = false;
  result.loadXML( xml );
  return result;
}

jQuery(document).ready(function() {

  var xml = parser( xmlSource );

  $(xml).find( "criteria>row" ).each( function() {
    if ( $("value", this).text() == "Ask User At Runtime" ) {
      var seq = $("seqno", this).text();
      $("#result").append( $("<p/>").text( "Check seqno: " + seq ) );
    }
  });

});

</script>
</head>
<body>

<div id="result"></div>

</body>
</html>

Open in new window

0
ScreenConnect 6.0 Free Trial

Explore all the enhancements in one game-changing release, ScreenConnect 6.0, based on partner feedback. New features include a redesigned UI, app configurations and chat acknowledgement to improve customer engagement!

 

Author Comment

by:robert_jenkin
ID: 34937905
both of these solutions seem to return only one instance..the xml could contina multiple rows with value = ask user at runtime. I need to get the xml containing the rows
using the following as example

<data>
<row id=1><value>Ask User At Runtime</value></row>
<row id=1><value>Ask User At Runtime</value></row>
<row id=1><value>xxx</value></row>
<row id=1><value>yyy</value></row>
</data>

I need to get back

<data>
<row id=1><value>Ask User At Runtime</value></row>
<row id=1><value>Ask User At Runtime</value></row>
</data>

Thanks
0
 
LVL 82

Expert Comment

by:leakim971
ID: 34937951
>both of these solutions seem to return only one instance..

Not in my first code, line  : var rows = $("value:contains('Ask User At Runtime')", $(xml)).parent("row");

To alert all the nodes :
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
<script language="javascript" src="http://code.jquery.com/jquery-1.5.js"></script>
<script language="javascript">
	var xml = "<criteria>";
	xml += "<row><operation>EQUALS0</operation><reportkey>C5EFEBE6-82E6-2365-7F11-854FF053CABC</reportkey><seqno>0</seqno><name>V_REPORTING.EXPIRY_DATE</name><value>Ask User0 At Runtime</value><code></code><operator></operator></row>";
	xml += "<row><operation>EQUALS</operation><reportkey>C5EFEBE6-82E6-2365-7F11-854FF053CABC</reportkey><seqno>0</seqno><name>V_REPORTING.EXPIRY_DATE</name><value>Ask User At Runtime</value><code></code><operator></operator></row>";
	xml += "<row><operation>EQUALS1</operation><reportkey>C5EFEBE6-82E6-2365-7F11-854FF053CABC</reportkey><seqno>0</seqno><name>V_REPORTING.EXPIRY_DATE</name><value>Ask User At Runtime</value><code></code><operator></operator></row>";
	xml += "</criteria>";
	$(document).ready(function() {
		var rows = $("value:contains('Ask User At Runtime')", $(xml)).parent("row");
		alert("we found " + rows.length + " row(s)");
		rows.each(function() {
			alert( $("operation", this).text() );
		});
	});
</script>
</head>
<body>
</body>
</html>

Open in new window

0
 

Author Comment

by:robert_jenkin
ID: 34937967
I tried to execute the code and I get 0 rows found
0
 
LVL 20

Accepted Solution

by:
Proculopsis earned 500 total points
ID: 34938000

This should do the trick for you:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8"/>
<title>http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/Q__26834524.html</title>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4/jquery.min.js"></script>
<script type="text/javascript"> 

var xmlSource = '<?xml version="1.0" encoding="utf-8"?>\
<criteria>\
  <row>\
    <operation>EQUALS</operation>\
    <reportkey>C5EFEBE6-82E6-2365-7F11-854FF053CABC</reportkey>\
    <seqno>0</seqno>\
    <name>V_REPORTING.EXPIRY_DATE</name>\
    <value>Ask User At Runtime</value>\
    <code></code>\
    <operator></operator>\
  </row>\
  <row>\
    <operation>EQUALS</operation>\
    <reportkey>C5EFEBE6-82E6-2365-7F11-854FF053CABC</reportkey>\
    <seqno>1</seqno>\
    <name>V_REPORTING.EXPIRY_DATE</name>\
    <value>0</value>\
    <code></code>\
    <operator></operator>\
  </row>\
  <row>\
    <operation>EQUALS</operation>\
    <reportkey>C5EFEBE6-82E6-2365-7F11-854FF053CABC</reportkey>\
    <seqno>2</seqno>\
    <name>V_REPORTING.EXPIRY_DATE</name>\
    <value>Ask User At Runtime</value>\
    <code></code>\
    <operator></operator>\
  </row>\
</criteria>\
';

function parser( xml )
{
  var result = new ActiveXObject( "Microsoft.XMLDOM" );
  result.async = false;
  result.loadXML( xml );
  return result;
}

jQuery(document).ready(function() {

  var xml = parser( xmlSource );

  $(xml).find( "criteria>row" ).each( function() {
    if ( $("value", this).text() != "Ask User At Runtime" ) {
      $(this).remove();
    }
  });

  $("#result").append( $("<pre/>").text( xml.xml ) );
  

});

</script>
</head>
<body>

<fieldset id="result"><legend>XML</legend></fieldset>

</body>
</html>

Open in new window

0
 
LVL 82

Expert Comment

by:leakim971
ID: 34938006
Fix for IE (testing page) :


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
<script language="javascript" src="http://code.jquery.com/jquery-1.5.js"></script>
<script language="javascript">
	var xml = "<criteria>";
	xml += "<row><operation>EQUALS0</operation><reportkey>C5EFEBE6-82E6-2365-7F11-854FF053CABC</reportkey><seqno>0</seqno><name>V_REPORTING.EXPIRY_DATE</name><value>Ask User0 At Runtime</value><code></code><operator></operator></row>";
	xml += "<row><operation>EQUALS</operation><reportkey>C5EFEBE6-82E6-2365-7F11-854FF053CABC</reportkey><seqno>0</seqno><name>V_REPORTING.EXPIRY_DATE</name><value>Ask User At Runtime</value><code></code><operator></operator></row>";
	xml += "<row><operation>EQUALS1</operation><reportkey>C5EFEBE6-82E6-2365-7F11-854FF053CABC</reportkey><seqno>0</seqno><name>V_REPORTING.EXPIRY_DATE</name><value>Ask User At Runtime</value><code></code><operator></operator></row>";
	xml += "</criteria>";
	$(document).ready(function() {
		var rows = $("value:contains('Ask User At Runtime')", "xml").parent("row");
		alert("we found " + rows.length + " row(s)");
		rows.each(function() {
			alert( $("operation", this).text() );
		});
	});
</script>
</head>
<body>
<xml>
<criteria>
<row><operation>EQUALS0</operation><reportkey>C5EFEBE6-82E6-2365-7F11-854FF053CABC</reportkey><seqno>0</seqno><name>V_REPORTING.EXPIRY_DATE</name><value>Ask User0 At Runtime</value><code></code><operator></operator></row>
<row><operation>EQUALS</operation><reportkey>C5EFEBE6-82E6-2365-7F11-854FF053CABC</reportkey><seqno>0</seqno><name>V_REPORTING.EXPIRY_DATE</name><value>Ask User At Runtime</value><code></code><operator></operator></row>
<row><operation>EQUALS1</operation><reportkey>C5EFEBE6-82E6-2365-7F11-854FF053CABC</reportkey><seqno>0</seqno><name>V_REPORTING.EXPIRY_DATE</name><value>Ask User At Runtime</value><code></code><operator></operator></row>
</criteria>
</xml>
</body>
</html>

Open in new window

0
 

Author Closing Comment

by:robert_jenkin
ID: 34938011
Thanks...
0
 

Author Comment

by:robert_jenkin
ID: 34938083
follow up... in review solution it appears to me that the jquery(document).ready() function is called on page loading.. how do I call it via a javascript function at a later time
0

Featured Post

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Have you tried to learn about Unicode, UTF-8, and multibyte text encoding and all the articles are just too "academic" or too technical? This article aims to make the whole topic easy for just about anyone to understand.
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
In this tutorial viewers will learn how to style a corner ribbon overlay for an image using CSS Create a new class by typing ".Ribbon":  Define the class' "display:" as "inline-block": Define its "position:" as "relative": Define its "overflow:" as …
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

773 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question