SSS - S3 Toolkit 0.9.6
S3 client library and applications
Loading...
Searching...
No Matches
Parsing

Response parser function for XML body and HTTP headers. More...

Data Structures

class  XMLOStream
 XMLGenerator. Use methods or overloaded subscript operator to insert data into XML tree. More...
 
class  XMLIStream
 XML parser, returns array of text elements with the same parent path or an array of parsed XML sub trees. More...
 

Functions

std::string sss::XMLTag (const std::string &xml, const std::string &tag)
 Extract and return content of XML tag.
 
std::vector< std::string > sss::XMLTags (const std::string &xml, const std::string &tag)
 Extract and return content of all XML tags matching word.
 
std::string sss::XMLTagPath (const std::string &xml, const std::string &path)
 Extract and return content of XML tag matching hierchical path.
 
std::string sss::HTTPHeader (const std::string &headers, const std::string &header)
 Extract and return HTTP header.
 
Headers sss::HTTPHeaders (const std::string &headers)
 Extract and return HTTP headers as key-value pairs.
 
MetaDataMap sss::MetaDataHeaders (const std::string &headers)
 Extract and return x-amz-meta-* headers.
 
std::string sss::TrimETag (const std::string &etag)
 Extract ETag string from returned etag text.
 
std::string FindElementText (const std::string &xml, const std::string &element)
 Return text inside XML tags.
 
std::string ParseXMLPath (const std::string &xml, const std::string &path)
 Extract text under XML path.
 
std::vector< std::string > ParseXMLMultiPathText (const std::string &xml, const std::string &prefixPpath, const std::string &suffixPath)
 Multilevel parsing function: return all elements matching path under prefix.
 
std::vector< std::unordered_map< std::string, std::string > > RecordList (const std::string &prefix, const std::unordered_map< std::string, std::vector< std::string > > &domMap)
 Extract records from DOM Map.
 
std::unordered_map< std::string, std::vector< std::string > > ParseXMLPathElementsText (const std::string &xml, const std::string &path)
 Return all elements at location grouped by element name.
 
std::unordered_map< std::string, std::vector< std::string > > DOMToDict (const std::string &xml)
 Convert XML text to {path, text element array} map. Input.
 
std::unordered_map< std::string, std::vector< std::string > > ExtractSubPaths (const std::string &prefix, const std::unordered_map< std::string, std::vector< std::string > > &domMap)
 Return map of {subpaths with same prefix, {list of text elements under subpaths}}.
 
std::string XMLToText (const tinyxml2::XMLDocument &doc, bool header=true, char eol_='\n', int indent=2, std::unordered_map< std::string, std::string > kv={})
 Transform DOM into text, replacing matching keywords with values specified in {keyword => value} map.
 
tinyxml2::XMLElement * CreatePath (tinyxml2::XMLElement *n, const std::string &path, const std::string &text="")
 Create XML tree from path format and place it under passed element. I.e. from {"/tag1/tag2", "text"} to "<tag1><tag2>text</tag2></tag1>".
 
tinyxml2::XMLElement * CreatePath (tinyxml2::XMLDocument &doc, const std::string &path, const std::string &text="")
 Create XML tree from path format and place inside document instance.
 
tinyxml2::XMLElement * CreatePaths (tinyxml2::XMLElement *n, const std::string &path, const std::vector< std::pair< std::string, std::string > > &paths)
 Create multiple XML trees under element. Invokes CreatePath multiple times passing {path, text} value at each invocation.
 
tinyxml2::XMLElement * CreatePaths (tinyxml2::XMLDocument &doc, const std::string &path, const std::vector< std::pair< std::string, std::string > > &paths)
 Create multiple XML trees inside tynyxml2 XML document. Invokes CreatePath multiple times passing {path, text} value at each invocation.
 
std::string Get (const XMLRecord &r, const std::string &path)
 

Detailed Description

Response parser function for XML body and HTTP headers.

High level XML parsing functions using filepath like input.

Function Documentation

◆ CreatePath()

tinyxml2::XMLElement * CreatePath ( tinyxml2::XMLElement *  n,
const std::string &  path,
const std::string &  text = "" 
)

Create XML tree from path format and place it under passed element. I.e. from {"/tag1/tag2", "text"} to "<tag1><tag2>text</tag2></tag1>".

Parameters
[in]npointer to root element
[in]pathtextual representation of XML path
[in]texttext element to append to path

◆ CreatePaths() [1/2]

tinyxml2::XMLElement * CreatePaths ( tinyxml2::XMLDocument &  doc,
const std::string &  path,
const std::vector< std::pair< std::string, std::string > > &  paths 
)

Create multiple XML trees inside tynyxml2 XML document. Invokes CreatePath multiple times passing {path, text} value at each invocation.

Parameters
[in]nroot element
[in]pathXML path in "/.../..." format
[in]pathslist of {path, text element value} to pass to CreatePath
Returns
XML tree

◆ CreatePaths() [2/2]

tinyxml2::XMLElement * CreatePaths ( tinyxml2::XMLElement *  n,
const std::string &  path,
const std::vector< std::pair< std::string, std::string > > &  paths 
)

Create multiple XML trees under element. Invokes CreatePath multiple times passing {path, text} value at each invocation.

Parameters
[in]nroot element
[in]pathXML path in "/.../..." format
[in]pathslist of {path, text element value} to pass to CreatePath
Returns
XML tree

◆ DOMToDict()

std::unordered_map< std::string, std::vector< std::string > > DOMToDict ( const std::string &  xml)

Convert XML text to {path, text element array} map. Input.

<tag1>
<tag2>
Text 1_2
</tag2>
<tag2>
Text 2_2
</tag2>
<othertag>
Other text
</othertag>
</tag1>

Output

unordered_map<string, string> domMap = {
{"/tag1/tag2", {"Text 1_2", "Text 2_2"},
{"/tag1/othertag", {"Other text"}}
};
Parameters
[in]xmlXML text
Returns
map of {path, element array} where the key is the path to the text elements stored in the value

◆ ExtractSubPaths()

std::unordered_map< std::string, std::vector< std::string > > ExtractSubPaths ( const std::string &  prefix,
const std::unordered_map< std::string, std::vector< std::string > > &  domMap 
)

Return map of {subpaths with same prefix, {list of text elements under subpaths}}.

Input

string path = "/path/to/some"
unordered_map<string, vector<string>> domMapInput = {
{"/path/to/some/textA", {"text A1", "text A2"}},
{"/path/to/some_other/text", {"other text"}},
{"/path/to/some/textB", {"text B1", "text B2", "text B3"}}
};
auto subPaths = ExtractSubPaths(path, domMapInput);
std::unordered_map< std::string, std::vector< std::string > > ExtractSubPaths(const std::string &prefix, const std::unordered_map< std::string, std::vector< std::string > > &domMap)
Return map of {subpaths with same prefix, {list of text elements under subpaths}}.

Output

unordered_map<string, vector<string>> subPaths = {
{"/textA", {"text A1", "text A2"}},
{"/textB", {"text B1", "text B2"}}
};
Parameters
[in]prefixXML prefix in path format: "/.../..."
[in]domMapDOM document in {prefix => text value} format
Returns
map of subpaths => text element list having prefix prefix

◆ FindElementText()

std::string FindElementText ( const std::string &  xml,
const std::string &  element 
)

Return text inside XML tags.

Parameters
[in]xmlXML text.
[in]elementname of XML tag to search for.
Returns
XML text element.

◆ Get()

std::string Get ( const XMLRecord &  r,
const std::string &  path 
)
inline

Extract string from map

Parameters
[in]rXMLRecord instance
See also
XMLRecord
Parameters
[in]pathkey in the form "/tag1/tag11..."
Returns
text or empty string if text not found or empty

◆ HTTPHeader()

std::string sss::HTTPHeader ( const std::string &  headers,
const std::string &  header 
)

Extract and return HTTP header.

Parameters
[in]headerstext containing the header section of an HTTP payload
[in]headerheader name
+ Here is the caller graph for this function:

◆ HTTPHeaders()

Headers sss::HTTPHeaders ( const std::string &  headers)

Extract and return HTTP headers as key-value pairs.

Parameters
[in]headerstext containing the header section of an HTTP payload
Returns
std::map<std::string, std::string> of {header name, header value} tuples
+ Here is the caller graph for this function:

◆ MetaDataHeaders()

MetaDataMap sss::MetaDataHeaders ( const std::string &  headers)

Extract and return x-amz-meta-* headers.

Parameters
[in]headerstext containing the header section of an HTTP payload
Returns
std::map<std::string, std::string> of {header name, header value} tuples

◆ ParseXMLMultiPathText()

std::vector< std::string > ParseXMLMultiPathText ( const std::string &  xml,
const std::string &  prefixPpath,
const std::string &  suffixPath 
)

Multilevel parsing function: return all elements matching path under prefix.

E.g.

If prefix is "/tag1/tag11" and suffix is "/name" it will extract text from all the <name>Name one</name><name>Name two</name> elements inside <tag1><tag2>..</tag2></tag1 equivalent to:

It groups text elements by XML prefix, mapping prefix to multiple text elements.

Parameters
[in]xmlXML text.
[in]prefixPathstarting path from element extraction.
[in]suffixPathreturn all text elements matching suffixPath path under prefixPath.
Returns
list of extracted text elements under /prefixPath/suffixPath/

◆ ParseXMLPath()

std::string ParseXMLPath ( const std::string &  xml,
const std::string &  path 
)

Extract text under XML path.

Parameters
[in]XMLtext.
[in]pathpath to XML tag in the format "/tag1/tag11/tag112...".
Returns
XML text element.

◆ ParseXMLPathElementsText()

std::unordered_map< std::string, std::vector< std::string > > ParseXMLPathElementsText ( const std::string &  xml,
const std::string &  path 
)

Return all elements at location grouped by element name.

Input

<tag1>
<tag11>
<node1>
Text1
</node1>
<node1>
Text1_2
</node1>
<node2>
Text2
</node2>
<node2>
Text2_2
</node2>
<tag11>
</tag1>

Output

unordered_map<string, vector<string>> elementsMap = {
{"node1", {"Text1", "Text1_2"}},
{"node2", {"Text2", "Text2_2"}}
};
Parameters
[in]xmlXML text
[in]pathpath to elements: "/tag1/tag12/...."
Returns
element text at path grouped by element name

◆ RecordList()

std::vector< std::unordered_map< std::string, std::string > > RecordList ( const std::string &  prefix,
const std::unordered_map< std::string, std::vector< std::string > > &  domMap 
)

Extract records from DOM Map.

See also
DOMToDict. A record is defined as a map with:
  • key = path to tag element containing text element in the format "/tag1/tag2/..."
  • value = text element under key path

Input:

  • domMap = XML text -> DOMToDict -> {"/listbucketresult/contents/key", "Key1"}...
  • prefixPath = "/listbucketresult/contents"
<?xml version="1.0" encoding="UTF-8"?>
<ListBucketResult
xmlns="http://s3.amazonaws.com/doc/2006-03-01/">
<Name>tst</Name>
<Prefix></Prefix>
<MaxKeys>1000</MaxKeys>
<IsTruncated>false</IsTruncated>
<Contents>
<Key>Key1</Key1>
<Size>67108864</Size>
<StorageClass>STANDARD</StorageClass>
<Owner>
<ID>Owner1</ID>
<DisplayName>Owner One</DisplayName>
</Owner>
<Type>Normal</Type>
</Contents>
<Contents>
<Key>Key2</Key>
<Size>4294967296</Size>
<StorageClass>STANDARD</StorageClass>
<Owner>
<ID>Owner1</ID>
<DisplayName>Owner One</DisplayName>
</Owner>
<Type>Normal</Type>
</Contents>
</ListBucketResult>

Output

If the case insensitive flag is set (default) then all tag names are converted to lowercase.

vector<unordered_map<string, string>> record = {
{
{"/key", "Key1"},
{"/size","67108864"},
{"/storageclass", "STANDARD"},
{"/owner/ID", "Owner1"},
{"/owner/displayname", "Owner One"}
{"/type", "Normal"}
},
{
{"/key", "Key2"},
{"/size","4294967296"},
{"/storageclass", "STANDARD"},
{"/owner/ID", "Owner1"},
{"/owner/displayname", "Owner One"}
{"/type", "Normal"}
}
};

Transpose operation:

FROM:

/bucket/creation/date date1
date2
date3
/bucket/name name1
name2

TO:

/bucket/creation/date date1
/bucket/name name1
/bucket/creation/date date2
/bucket/name name2
/bucket/creation/date date3
/bucket/name ""
Parameters
[in]prefixXML path to record data.
[in]domMapXML document transformed into {"path", "text"} map through DOMToDict function.
Returns
list of record maps, each map contains the {field name, field value} entries for one record.
+ Here is the caller graph for this function:

◆ TrimETag()

std::string sss::TrimETag ( const std::string &  etag)

Extract ETag string from returned etag text.

Parameters
[in]etagetag text
Returns
etag string without quotes

◆ XMLTag()

std::string sss::XMLTag ( const std::string &  xml,
const std::string &  tag 
)

Extract and return content of XML tag.

Parameters
[in]xmlXML text
[in]tag <tag> name
Returns
<tag> content
+ Here is the caller graph for this function:

◆ XMLTagPath()

std::string sss::XMLTagPath ( const std::string &  xml,
const std::string &  path 
)

Extract and return content of XML tag matching hierchical path.

Parameters
[in]xmlXML text
[in]pathtag path separated by '/' character
Returns
content of tag if found, empty string otherwise

◆ XMLTags()

std::vector< std::string > sss::XMLTags ( const std::string &  xml,
const std::string &  tag 
)

Extract and return content of all XML tags matching word.

Parameters
[in]xmlXML text
[in]tag <tag> name
Returns
content of each tag matching name

◆ XMLToText()

std::string XMLToText ( const tinyxml2::XMLDocument &  doc,
bool  header = true,
char  eol_ = '\n',
int  indent = 2,
std::unordered_map< std::string, std::string >  kv = {} 
)

Transform DOM into text, replacing matching keywords with values specified in {keyword => value} map.

Parameters
[in]docXML document in tinyxml2 format
[in]kvkeyword => value map, keywords are replaced with values in translated text
[in]headerif true addx the <xml...> header
[in]eolend of line separator, default is '
', if '0' no EOL generated
[in]indentindentation value
Returns
XML text with keywords replaced with values specified in input map
+ Here is the caller graph for this function: