ethan.whitt Posted April 2, 2010 Share Posted April 2, 2010 Hello, I have large batch of unique XML files. I need to regex match any tag that contains "error" or "drop" within the file. Once matched, I then need to extract the value and verify that it equals zero. If the value is not zero, I then need to print the tag name and value. Any help would be appreciated. Thanks! ~ew Quote Link to comment https://forums.phpfreaks.com/topic/197299-how-to-regex-search-tags-in-xml-file/ Share on other sites More sharing options...
teamatomic Posted April 2, 2010 Share Posted April 2, 2010 A sample of the file might help, but... preg_match('~<error>(.*?)<~',$line,$match); $found=$match[1]; HTH Teamatomic Quote Link to comment https://forums.phpfreaks.com/topic/197299-how-to-regex-search-tags-in-xml-file/#findComment-1035627 Share on other sites More sharing options...
ethan.whitt Posted April 2, 2010 Author Share Posted April 2, 2010 Attached is an example of the data. Anything tag that contains any iteration of "error" should be considered. ~ew [attachment deleted by admin] Quote Link to comment https://forums.phpfreaks.com/topic/197299-how-to-regex-search-tags-in-xml-file/#findComment-1035750 Share on other sites More sharing options...
salathe Posted April 2, 2010 Share Posted April 2, 2010 So given the example data attached above, which error tags and values would you want to print? Quote Link to comment https://forums.phpfreaks.com/topic/197299-how-to-regex-search-tags-in-xml-file/#findComment-1035866 Share on other sites More sharing options...
teamatomic Posted April 2, 2010 Share Posted April 2, 2010 $i=0; foreach($lines as $line) { $line=trim($line); if(preg_match('~\<(.*?\-errors)>(.+?)\</.*?\-errors\>~i',$line,$matches[$i])) $i++; } print_r($matches); HTH Teamatomic Quote Link to comment https://forums.phpfreaks.com/topic/197299-how-to-regex-search-tags-in-xml-file/#findComment-1036094 Share on other sites More sharing options...
ethan.whitt Posted April 2, 2010 Author Share Posted April 2, 2010 Hey Salathe, I am trying to match *ANY* tag that contains "error" (i.e. -error, error-, -error-, ...etc), at any level of the document. ~ew Quote Link to comment https://forums.phpfreaks.com/topic/197299-how-to-regex-search-tags-in-xml-file/#findComment-1036119 Share on other sites More sharing options...
salathe Posted April 2, 2010 Share Posted April 2, 2010 Even the ones which contain other tags which contain "error": what about if the tag content is "0", "none" or something else? Quote Link to comment https://forums.phpfreaks.com/topic/197299-how-to-regex-search-tags-in-xml-file/#findComment-1036181 Share on other sites More sharing options...
ethan.whitt Posted April 2, 2010 Author Share Posted April 2, 2010 The XML file is an output of counters and their respective values. Since the counters can be in different levels within the file, I need to recursively look through all levels. If a tag is found with "error | runt | whatever" and the counter value _IS_ zero or non-numeric, ignore it. If the counter value is greater than 1, then only display that tag, regardless of the levels above and it's value. I have been looking for an xpath function that will do this, but so far, no luck. I think the terms I am using in Google are simply wrong. Quote Link to comment https://forums.phpfreaks.com/topic/197299-how-to-regex-search-tags-in-xml-file/#findComment-1036199 Share on other sites More sharing options...
salathe Posted April 3, 2010 Share Posted April 3, 2010 I have been looking for an xpath function that will do this, but so far, no luck. One such XPath query could look like: //*[contains(local-name(), "error") and . > 0] The above asks for all nodes whose tag name contains the word "error" and has a numeric value greater than zero. Note that in the sample XML document provided, none of the nodes match that criteria. Here's a quickie example script using that query and printing the matching nodes out. <?php // Load XML file $xml = new SimpleXMLElement('example_data.txt', NULL, TRUE); // Search for non-zero errors without child nodes $errors = $xml->xpath('//*[contains(local-name(), "error") and . > 0]'); // Print out those errors printf("Found %d errors" . PHP_EOL, count($errors)); foreach ($errors as $error) { echo $error->saveXML() . PHP_EOL; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/197299-how-to-regex-search-tags-in-xml-file/#findComment-1036358 Share on other sites More sharing options...
ethan.whitt Posted April 5, 2010 Author Share Posted April 5, 2010 Salathe, Thanks! This was enlightening. I can not figure out how to display the tag to each non-zero match. Say you had this xml as the source file. <a> <b> <test-error>19</test-error> </b> </b> How would you enable this result? Found 1 errors test-error 19 Thanks! ~ew Quote Link to comment https://forums.phpfreaks.com/topic/197299-how-to-regex-search-tags-in-xml-file/#findComment-1037040 Share on other sites More sharing options...
salathe Posted April 5, 2010 Share Posted April 5, 2010 Use getName() for the tag and get the element as a string for its value. For example: foreach ($errors as $error) { $tag = $error->getName(); $num = (string) $error; echo $tag . " " . $num . PHP_EOL; } Quote Link to comment https://forums.phpfreaks.com/topic/197299-how-to-regex-search-tags-in-xml-file/#findComment-1037118 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.