Showing posts with label XPATH. Show all posts
Showing posts with label XPATH. Show all posts

Wednesday, October 17, 2012

Using The Solarwinds IPmonitor API WSDL Webservice With SOAP And XPATH

<?php
function dumpDOMnodeList($xml)
{
    //USED TO DUMP THE XPATH RESULTS
    $docTmp = new DOMDocument();
    foreach($xml as $n) $docTmp->appendChild($docTmp->importNode($n,true));
    print_r($docTmp->saveHTML());
}

$soapVersion = '1.2';
$soapLocation = 'https://domain.com/soap/status.asmx?WSDL';
$soapContext = stream_context_create(array('ssl' => array('verify_peer' => FALSE)));
$soapParameters = array (
            'location' => $soapLocation,
            'login' => 'login',
            'password' => 'password',
            'stream_context' => $soapContext
            );         

//IPM.WSDL IS A LOCAL COPY OF STATUS.ASMX?WSDL
$soapClient = new SoapClient('ipm.wsdl',$soapParameters); 

//GET GROUPS
/*
$soapRequest = '<?xml version="1.0" encoding="utf-8"?>';
$soapAction = 'http://schemas.ipmonitor.com/ipm70/GetGroups';
*/

//GET MONITORS
/*MAIN GROUP WITHIN IPM, NEVER CHANGES CAN BE OBTAINED BY USING "GetGroups" METHOD... 
IT IS THE MAIN gtGroup ID WRAPPING THE RESPONSE*/
$groupID = 'XXXXXXXXXXXX'; 
$justTrouble = 'TRUE'; // ONLY RETURN DISTRESSED MONITORS

$soapRequest = '<?xml version="1.0" encoding="utf-8"?>'.$groupID.''.$justTrouble.'';
$soapAction = 'http://schemas.ipmonitor.com/ipm70/GetMonitors';

$soapResult = $soapClient->__doRequest($soapRequest, $soapLocation, $soapAction, $soapVersion); //REQUEST, LOCATION, ACTION, VERSION

$doc = new DOMDocument();
$doc->LoadXML($soapResult);

$xpath = new DOMXpath($doc);
$xpath->registerNamespace('soap', 'http://schemas.xmlsoap.org/soap/envelope/');
$xpath->registerNamespace('def', 'http://schemas.ipmonitor.com/ipm70/'); 

$alerts = $xpath->query("/soap:Envelope/soap:Body/def:GetMonitorsResponse/def:GetMonitorsResult/def:rtMonitor");

dumpDOMnodeList($alerts);
?>

Friday, September 16, 2011

Iterate Over Nested / Multi-Level DOM NodeList

<?php
function processNodeList($nodeList)
{
    # text nodes don't have child nodes
    if ($root->nodeName == '#text')
    {
        $xml = $root->nodeValue;
    }
    else
    {
        # start tag
        $xml = sprintf('<%s>', $root->nodeName);
        # contents
        foreach ($root->childNodes as $node)
        {
            $xml .= processNodeList($node);
        }
        #end tag
        $xml .= sprintf('', $root->nodeName);
        # for some reason the blog puts html comment tags around the closing sprintf, just FYI to remove those as they are not intended
    }
    return $xml;
}

$doc = new DOMDocument();
$doc->loadXML($xml);
$xpath = new DOMXPath($doc);

$nodeList = $xpath->query('/');
$xml = processNodeList($nodeList);

echo $xml; 
?>

Thursday, September 1, 2011

Import Xpath Query NodeList To A New XML Document

<?php

$doc = new DOMDocument();
$doc->loadxml($xml); //XML STRING
$xpath = new DOMXpath($doc);
$xpathResults = $xpath->query(""); //YOUR QUERY
$nodes = $xpathResults->length;

if($nodes > 0)
{
 $newDom = new DOMDocument('1.0','UTF-8');
 $root = $newDom->createElement('root');
 $root = $newDom->appendChild($root);
 
 foreach ($xpathResults as $domElement)
 {
  $domNode = $newDom->importNode($domElement, true);
  $root->appendChild($domNode);
  $newXML = $newDom->saveXML();     
 }   
 header("Content-type: application/xml");
 echo $newXML;
} 

?>

Friday, January 22, 2010

Conditionally query specific elements within an XML document

Given:

     the name
     the desc
     misc garbage
     1


Suppose you had 100 of these and wanted to obtain just the name and description for a specific id

<?php
$doc = simplexml_load_string();

$results = $doc->xpath("//tree[contains(id,'1')]/child::*[self::name or self::desc]"); 

?>

That will get the job done

Nested queries with DOM and Xpath

<?php
//CREATE NEW DOM INSTANCE
$doc = new DOMDocument();

//LOAD XML FROM FILE
$doc->load('data.xml');

//CREATE A NEW XPATH INSTANCE
$xpath = new DOMXpath($doc);

//ASSIGN FIRST XPATH TO VARIABLE
$containers = $xpath->query('/item/container');

//COUNT THE NUMBER OF NODELISTS
$count = $containers->length;

//LOOP THROUGH THE NODELISTS
for($i=0;$i<$count;$i++)
{
   //CREATE ANOTHER XPATH INSTANCE BASED ON THE PREVIOUS
   $subitems = $xpath->query('.//subitem',$containers->item($i));

   //COUNT THE NUMBER OF SUB-NODELISTS
   $subcount = $subitems->length;

   //LOOP THROUGH AND ACCESS ITEMS PER USUAL
   for($j=0;$j<$subcount;$j++)
   {
      //echo $containers->item($i)->...;
      //echo $subitems->item($j)->...;
   }
}
?>