Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

How to search XML to find rows containing a value

Posted on 2011-02-20
10
Medium Priority
?
265 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 83

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 83

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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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 83

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 83

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

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

Question has a verified solution.

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

This article discusses four methods for overlaying images in a container on a web page
Originally, this post was published on Monitis Blog, you can check it here . Websites are getting bigger and more complicated by the day. Video, images and custom fonts are all great for showcasing your product or service. But the price to pay in…
In this tutorial viewers will learn how to code links for mobile sites that, once clicked, send a call or text to a specified number. For a telephone link (once clicked, calls a number), begin with a normal "<a href=" link tag. For the href, specify…
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.
Suggested Courses

926 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