Solved

How to search XML to find rows containing a value

Posted on 2011-02-20
10
255 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
 

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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

JavaScript can be used in a browser to change parts of a webpage dynamically. It begins with the following pattern: If condition W is true, do thing X to target Y after event Z. Below are some tips and tricks to help you get started with JavaScript …
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
In this tutorial viewers will learn how to position items using CSS's three positioning types Create a new HTML document with an internal stylesheet.: Create another div in CSS and name it Absolute : Type "position:absolute;" and "top:10px; left:50p…
In this tutorial viewers will learn how to embed videos in a webpage using HTML5. Ensure your DOCTYPE declaration is set to HTML5: "<!DOCTYPE html>": Use the <video> tag to insert a video. Define the src as the URL of your video; this is similar to …

911 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now