Exemple #1
0
void ossimXmlNode::findChildNodes(const ossimString& xpath,
                                  ossimXmlNode::ChildListType& result)const
{
    //***
    // Scan for trivial result (no children owned):
    //***
    if (theChildNodes.size() == 0)
        return;

    //***
    // Make a copy to manipulate:
    //***
    ossimString rel_xpath (xpath);
    if (rel_xpath.empty())
        return;

    //---
    // First verify that this is not an absolute path:
    //---
    if (rel_xpath[static_cast<std::string::size_type>(0)] ==
            XPATH_DELIM[static_cast<std::string::size_type>(0)])
    {
        ossimNotify(ossimNotifyLevel_WARN)
                << "WARNING: ossimXmlNode::findChildNodes\n"
                << "Only relative XPaths can be searched from a node. "
                << "Returning null list...\n";
        return;
    }

    //***
    // Read the desired tag from the relative xpath
    //***
    ossimString desired_tag (rel_xpath);
    if (rel_xpath.contains(XPATH_DELIM))
        desired_tag = rel_xpath.before(XPATH_DELIM);
    ossimString sub_xpath (rel_xpath.after(XPATH_DELIM));

    //***
    // Loop over all child nodes for match:
    //***
    ossimXmlNode::ChildListType::const_iterator child_iter = theChildNodes.begin();
    while(child_iter != theChildNodes.end())
    {
        if ((*child_iter)->getTag() == desired_tag)
        {
            if (sub_xpath.empty())
            {
                //***
                // This was the final target node, simply append to the result:
                //***
                result.push_back(*child_iter);
            }
            else
            {
                //***
                // This match identifies a possible tree to search given the
                // remaining xpath (sub_xpath). Query this child node to search
                // its tree for the remaining xpath:
                //***
                (*child_iter)->findChildNodes(sub_xpath, result);
            }
        }

        //***
        // Proceed to next child:
        //***
        child_iter++;
    }
}
Exemple #2
0
ossimRefPtr<ossimXmlNode> ossimXmlNode::findFirstNode(const ossimString& xpath)
{
    if(theChildNodes.size() < 1) return 0;
    //
    // Make a copy to manipulate:
    //
    ossimString rel_xpath (xpath);
    if (rel_xpath.empty())
        return 0;

    //
    // First verify that this is not an absolute path:
    //
    if (rel_xpath[static_cast<std::string::size_type>(0)] ==
            XPATH_DELIM[static_cast<std::string::size_type>(0)])
    {
        ossimNotify(ossimNotifyLevel_WARN)
                << "WARNING: ossimXmlNode::findChildNodes\n"
                << "Only relative XPaths can be searched from a node. "
                << "Returning null list...\n";
        return 0;
    }

    //
    // Read the desired tag from the relative xpath
    //
    ossimString desired_tag (rel_xpath);
    if (rel_xpath.contains(XPATH_DELIM))
        desired_tag = rel_xpath.before(XPATH_DELIM);
    ossimString sub_xpath (rel_xpath.after(XPATH_DELIM));

    ossimRefPtr<ossimXmlNode> result = 0;

    //
    // Loop over all child nodes for match:
    //
    ossimXmlNode::ChildListType::iterator child_iter = theChildNodes.begin();
    while((child_iter != theChildNodes.end())&&
            (!result.valid()))
    {
        if ((*child_iter)->getTag() == desired_tag)
        {
            if (sub_xpath.empty())
            {
                return *child_iter;
            }
            else
            {
                //
                // This match identifies a possible tree to search given the
                // remaining xpath (sub_xpath). Query this child node to search
                // its tree for the remaining xpath:
                //
                result = (*child_iter)->findFirstNode(sub_xpath);
            }
        }

        //
        // Proceed to next child:
        //
        ++child_iter;
    }

    return result;
}
void ossimXmlDocument::findNodes(const ossimString& arg_xpath,
                            vector<ossimRefPtr<ossimXmlNode> >& result) const
{
   //
   // First verify the root node exists:
   //
   if (!theRootNode.valid())
   {
      if (traceDebug())
      {
         ossimNotify(ossimNotifyLevel_DEBUG)
            << "WARNING: ossimXmlDocument::findNodes,\n"
            << "No root node has been instantiated. Returning null "
            << "node list..." << endl;
      }
      return;
   }

   //
   // Make a copy to manipulate:
   //
   ossimString xpath (arg_xpath);
   if (xpath.empty())
      return;
   
   //
   // Check if absolute path:
   //
   if (xpath[static_cast<std::string::size_type>(0)] !=
       XPATH_DELIM[static_cast<std::string::size_type>(0)])
   {
      if (traceDebug())
      {
         ossimNotify(ossimNotifyLevel_DEBUG)
            << "WARNING: ossimXmlDocument::findNodes\n"
            << "Only absolute XPaths are supported. Returning null "
            << "node list..." << endl;
      }
      return;
   }

   //
   // Check that root tag matches path root:
   //
   ossimString rel_xpath (xpath.after(XPATH_DELIM));
   ossimString root_tag (rel_xpath);
   if (root_tag.contains(XPATH_DELIM))
       root_tag = rel_xpath.before(XPATH_DELIM);
   
   if (root_tag != theRootNode->getTag())
   {
      if (traceDebug())
      {
         ossimNotify(ossimNotifyLevel_DEBUG)
            << "WARNING: ossimXmlDocument::findNodes\n"
            << "XPath's root node <"<<root_tag<<"> does not match the "
            << "stored root node's tag <" << theRootNode->getTag() << ">. "
            << "Returning null node list..." << endl;
      }
      return;
   }

   //
   // If the root node was the requested node, return it alone:
   //
   rel_xpath = rel_xpath.after(XPATH_DELIM);
   if (rel_xpath.empty())
   {
      result.push_back(theRootNode);
      return;
   }
   
   //
   // Pass the node request on to the root node with the relative path:
   //
   theRootNode->findChildNodes(rel_xpath, result);
}