Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

How to search XML to find rows containing a value

Posted on 2011-02-20
10
Medium Priority
?
264 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Build and deliver software with DevOps

A digital transformation requires faster time to market, shorter software development lifecycles, and the ability to adapt rapidly to changing customer demands. DevOps provides the solution.

 

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 2000 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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

A while back, I ran into a situation where I was trying to use the calculated columns feature in SharePoint 2013 to do some simple math using values in two lists. Between certain data types not being accessible, and also with trying to make a one to…
In this blog, we’ll look at how improvements to Percona XtraDB Cluster improved IST performance.
In this Micro Tutorial viewers will learn how to create navigation buttons that change on rollover, using CSS (Continuation of the CSS Image Sprite tutorial) Create a parent ID for all the list items       - Specify position: absolute and display: block…
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 …

722 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