1
0
mirror of https://github.com/pgbackrest/pgbackrest.git synced 2025-03-03 14:52:21 +02:00

Add xmlNodeAttribute() to XmlNode object.

Retrieves a node attribute unless it is the root node.
This commit is contained in:
David Steele 2018-11-28 18:10:54 -05:00
parent 7c2fcb63e4
commit 47687dd13a
3 changed files with 32 additions and 1 deletions

View File

@ -142,6 +142,32 @@ xmlNodeLstAdd(XmlNodeList *this, xmlNodePtr node)
FUNCTION_TEST_RESULT(XML_NODE_LIST, this);
}
/***********************************************************************************************************************************
Get node attribute
***********************************************************************************************************************************/
String *
xmlNodeAttribute(XmlNode *this, const String *name)
{
FUNCTION_TEST_BEGIN();
FUNCTION_TEST_PARAM(XML_NODE, this);
FUNCTION_TEST_PARAM(STRING, name);
FUNCTION_TEST_ASSERT(this != NULL);
FUNCTION_TEST_ASSERT(name != NULL);
FUNCTION_TEST_END();
String *result = NULL;
xmlChar *value = xmlGetProp(this->node, (unsigned char *)strPtr(name));
if (value != NULL)
{
result = strNew((char *)value);
xmlFree(value);
}
FUNCTION_TEST_RESULT(STRING, result);
}
/***********************************************************************************************************************************
Get node content
***********************************************************************************************************************************/

View File

@ -38,6 +38,7 @@ void xmlDocumentFree(XmlDocument *this);
/***********************************************************************************************************************************
Node Getters
***********************************************************************************************************************************/
String *xmlNodeAttribute(XmlNode *this, const String *name);
XmlNode *xmlNodeChild(XmlNode *this, const String *name, bool errorOnMissing);
XmlNodeList *xmlNodeChildList(XmlNode *this, const String *name);
XmlNode *xmlNodeChildN(XmlNode *this, const String *name, unsigned int index, bool errorOnMissing);

View File

@ -21,7 +21,7 @@ testRun(void)
xmlDocumentNewZ(
"<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
"<ListBucketResult xmlns=\"http://s3.amazonaws.com/doc/2006-03-01/\">\n"
" <Name>bucket</Name>\n"
" <Name id=\"test\">bucket</Name>\n"
" <Prefix/>\n"
" <KeyCount>2</KeyCount>\n"
" <MaxKeys>1000</MaxKeys>\n"
@ -79,6 +79,10 @@ testRun(void)
"unable to find child 'Contents':2 in node 'ListBucketResult'");
TEST_RESULT_PTR(xmlNodeChildN(rootNode, strNew("Contents"), 2, false), NULL, "get missing child without error");
TEST_RESULT_PTR(xmlNodeAttribute(rootNode, strNew(BOGUS_STR)), NULL, "attempt to get missing attribute");
TEST_RESULT_STR(
strPtr(xmlNodeAttribute(xmlNodeChild(rootNode, strNew("Name"), true), strNew("id"))), "test", "get attribute");
TEST_RESULT_VOID(xmlDocumentFree(xmlDocument), "free xmldoc");
TEST_RESULT_VOID(xmlDocumentFree(NULL), "free null xmldoc");
}