Solved

How to search XML to find rows containing a value

Posted on 2011-02-20
10
252 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
How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

 

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 Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Building a website can seem like a daunting task to the uninitiated but it really only requires knowledge of two basic languages: HTML and CSS.
This article explains how to prepare an HTML email signature template file containing dynamic placeholders for users' Azure AD data. Furthermore, it explains how to use this file to remotely set up a department-wide email signature policy in Office …
In this tutorial viewers will learn how to style transparent/translucent elements using alpha transparency in CSS Start with a normal styled element, such as a div.: Define its "background-color" property as "rgba (255, 255, 255, .5): The numbers in…
The viewer will learn the benefit of using external CSS files and the relationship between class and ID selectors. Create your external css file by saving it as style.css then set up your style tags: (CODE) Reference the nav tag and set your prop…

746 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

12 Experts available now in Live!

Get 1:1 Help Now