Example #1
0
/**
 * Parses the given XML String and returns the main XMLNode
 * @param xml_string XML String
 * @param tag (?)
 * @param pResults XMLResult object to write in on error or success
 * @return The main XMLNode or empty XMLNode on error
 */
XMLNode *
XML::ParseString(const TCHAR *xml_string, Results *pResults)
{
  // If String is empty
  if (xml_string == nullptr) {
    // If XML::Results object exists
    if (pResults) {
      // -> Save the error type
      pResults->error = eXMLErrorNoElements;
      pResults->line = 0;
      pResults->column = 0;
    }

    // -> Return empty XMLNode
    return nullptr;
  }

  Error error;
  XMLNode xnode = XMLNode::Null();
  Parser xml = { nullptr, 0, eXMLErrorNone, nullptr, 0, true, };

  xml.lpXML = xml_string;

  // Fill the XMLNode xnode with the parsed data of xml
  // note: xnode is now the document node, not the main XMLNode
  ParseXMLElement(xnode, &xml);
  error = xml.error;

  // If the document node does not have childnodes
  XMLNode *child = xnode.GetFirstChild();
  if (child == nullptr) {
    // If XML::Results object exists
    if (pResults) {
      // -> Save the error type
      pResults->error = eXMLErrorNoElements;
      pResults->line = 0;
      pResults->column = 0;
    }

    // -> Return empty XMLNode
    return nullptr;
  } else {
    // Set the document's first childnode as new main node
    xnode = std::move(*child);
  }

  // If the new main node is the xml declaration
  // -> try to take the first childnode again
  if (xnode.IsDeclaration()) {
    // If the declaration does not have childnodes
    child = xnode.GetFirstChild();
    if (child == nullptr) {
      // If XML::Results object exists
      if (pResults) {
        // -> Save the error type
        pResults->error = eXMLErrorNoElements;
        pResults->line = 0;
        pResults->column = 0;
      }

      // -> Return empty XMLNode
      return nullptr;
    } else {
      // Set the declaration's first childnode as new main node
      xnode = std::move(*child);
    }
  }

  // If an XML::Results object exists
  // -> save the result (error/success)
  if (pResults) {
    pResults->error = error;

    // If we have an error
    if (error != eXMLErrorNone) {
      // Find which line and column it starts on and
      // save it in the XML::Results object
      CountLinesAndColumns(xml.lpXML, xml.nIndex, pResults);
    }
  }

  // If error occurred -> set node to empty
  if (error != eXMLErrorNone)
    return nullptr;

  // Return the node (empty, main or child of main that equals tag)
  return new XMLNode(std::move(xnode));
}
Example #2
0
/**
 * Parses the given XML String (lpszXML) and returns the main XMLNode
 * @param lpszXML XML String
 * @param tag (?)
 * @param pResults XMLResult object to write in on error or success
 * @return The main XMLNode or empty XMLNode on error
 */
XMLNode *
XMLNode::parseString(const TCHAR *lpszXML, XMLResults *pResults)
{
  // If String is empty
  if (!lpszXML) {
    // If XMLResults object exists
    if (pResults) {
      // -> Save the error type
      pResults->error = eXMLErrorNoElements;
      pResults->nLine = 0;
      pResults->nColumn = 0;
    }

    // -> Return empty XMLNode
    return NULL;
  }

  enum XMLError error;
  XMLNode xnode(NULL, false);
  struct XML xml = { NULL, 0, eXMLErrorNone, NULL, 0, NULL, 0, true, };

  xml.lpXML = lpszXML;

  // Fill the XMLNode xnode with the parsed data of xml
  // note: xnode is now the document node, not the main XMLNode
  xnode.ParseXMLElement(&xml);
  error = xml.error;

  // If the document node does not have childnodes
  const XMLNode *child = xnode.GetFirstChild();
  if (child == NULL) {
    // If XMLResults object exists
    if (pResults) {
      // -> Save the error type
      pResults->error = eXMLErrorNoElements;
      pResults->nLine = 0;
      pResults->nColumn = 0;
    }

    // -> Return empty XMLNode
    return NULL;
  } else {
    // Set the document's first childnode as new main node
    xnode = XMLNode(*child);
  }

  // If the new main node is the xml declaration
  // -> try to take the first childnode again
  if (xnode.isDeclaration()) {
    // If the declaration does not have childnodes
    child = xnode.GetFirstChild();
    if (child == NULL) {
      // If XMLResults object exists
      if (pResults) {
        // -> Save the error type
        pResults->error = eXMLErrorNoElements;
        pResults->nLine = 0;
        pResults->nColumn = 0;
      }

      // -> Return empty XMLNode
      return NULL;
    } else {
      // Set the declaration's first childnode as new main node
      xnode = XMLNode(*child);
    }
  }

  // If an XMLResults object exists
  // -> save the result (error/success)
  if (pResults) {
    pResults->error = error;

    // If we have an error
    if (error != eXMLErrorNone) {
      // Find which line and column it starts on and
      // save it in the XMLResults object
      CountLinesAndColumns(xml.lpXML, xml.nIndex, pResults);
    }
  }

  // If error occurred -> set node to empty
  if (error != eXMLErrorNone)
    return NULL;

  // Return the node (empty, main or child of main that equals tag)
  return new XMLNode(xnode);
}