# Construct hkeypath from string(xml path)

**URL:** <https://dmap-community.ductus.global/t/construct-hkeypath-from-string-xml-path/83>\
**Category:** CDB and CDB API\
**Created:** [April 30, 2015, 8:43am UTC](https://dmap-community.ductus.global/t/construct-hkeypath-from-string-xml-path/83 "2015-04-30T08:43:07Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![alext](https://avatars.discourse-cdn.com/v4/letter/a/f07891/32.png) [@alext](https://dmap-community.ductus.global/u/alext)\
**Post date:** [April 30, 2015, 8:43am UTC](https://dmap-community.ductus.global/t/construct-hkeypath-from-string-xml-path/83/1 "2015-04-30T08:43:07Z")

</div>

How I construct hkeypath from string(xml path) ?  
I tried to use maapi\_xpath2kpath. But I get following error:  
TRACE MAAPI\_XPATH2HKP DEBUG Operation not implemented - Unknown CDB operation

---

<div class="post-metadata">

**Author:** ![vleijon](https://avatars.discourse-cdn.com/v4/letter/v/74df32/32.png) [@vleijon](https://dmap-community.ductus.global/u/vleijon)\
**Post date:** [April 30, 2015, 9:10am UTC](https://dmap-community.ductus.global/t/construct-hkeypath-from-string-xml-path/83/2 "2015-04-30T09:10:07Z")

</div>

It depends a bit on what you want to do, can you explain in a little bit more detail.

I have never used the maapi function you mentioned, I have however used confd\_format\_keypath, which might be what you want.

Here is a code example of two different use cases:

> ```
> confd_value_t wbuf;
> CONFD_SET_STR(&wbuf, "www");
> struct in_addr in;
> inet_pton(AF_INET, "127.0.0.1", &in);
> n = confd_format_keypath(buf2, 255,
> "/mtest:mtest/%s/server{%x}/port/%ip4/gg",
> "servers", &wbuf, &in);
> n = confd_format_keypath(buf2, 255, "/mtest");
> 
> ```

---

<div class="post-metadata">

**Author:** ![alext](https://avatars.discourse-cdn.com/v4/letter/a/f07891/32.png) [@alext](https://dmap-community.ductus.global/u/alext)\
**Post date:** [April 30, 2015, 9:44am UTC](https://dmap-community.ductus.global/t/construct-hkeypath-from-string-xml-path/83/3 "2015-04-30T09:44:37Z")

</div>

I need a function that will take a string path and return a confd\_hkeypath\_t  
confd\_format\_keypath returns a string

In other words I need a function that performs reverse of confd\_pp\_kpath()

---

<div class="post-metadata">

**Author:** ![greg](https://avatars.discourse-cdn.com/v4/letter/g/71c47a/32.png) [@greg](https://dmap-community.ductus.global/u/greg)\
**Post date:** [April 30, 2015, 10:30pm UTC](https://dmap-community.ductus.global/t/construct-hkeypath-from-string-xml-path/83/4 "2015-04-30T22:30:50Z")

</div>

Alex,  
Why do you need to produce a confd\_hkeypath\_t? Normally ConfD passes to you a keypath in hashed form; I’m not aware of any API call that requires you to pass a hashed keypath.

That said, there is one way I could see doing this. If you are writing application code using the CDB API, you could use cdb\_cd using the string keypath to set the current working directory, then use cdb\_getcwd\_kpath to get the hashed keypath. MAAPI also has a similar pair of api calls that could be used to do this.

---

<div class="post-metadata">

**Author:** ![alext](https://avatars.discourse-cdn.com/v4/letter/a/f07891/32.png) [@alext](https://dmap-community.ductus.global/u/alext)\
**Post date:** [May 1, 2015, 8:26am UTC](https://dmap-community.ductus.global/t/construct-hkeypath-from-string-xml-path/83/5 "2015-05-01T08:26:52Z")

</div>

Hi Greg,

Using cdb\_cd and cdb\_getcwd\_kpath solved my problem. Thank you very much.

I am using the following code in order to retrieve the enum key names:

struct confd\_cs\_node \*node;  
confd\_hkeypath\_t \*kp;  
confd\_value\_t newv;

cdb\_get(rsock, &newv, path);  
cdb\_cd(rsock, path);  
cdb\_getcwd\_kpath(rsock, &kp);  
node = confd\_find\_cs\_node(kp, kp-\>len);  
confd\_val2str(node-\>info.type, &newv, fieldValue, sizeof(fieldValue));

---

<div class="post-metadata">

**Author:** ![per](https://avatars.discourse-cdn.com/v4/letter/p/9f8e36/32.png) [@per](https://dmap-community.ductus.global/u/per)\
**Post date:** [May 2, 2015, 1:16am UTC](https://dmap-community.ductus.global/t/construct-hkeypath-from-string-xml-path/83/6 "2015-05-02T01:16:12Z")

</div>

alext wrote:

> cdb\_cd(rsock, path);  
> cdb\_getcwd\_kpath(rsock, &kp);  
> node = confd\_find\_cs\_node(kp, kp-\>len);

It seems you overlooked the confd\_cs\_node\_cd() function. Using that, the code above becomes just (assuming that ‘path’ is an absolute path):

node = confd\_cs\_node\_cd(NULL, path);

I.e. as greg wrote, you are never **required** to pass a hkeypath - confd\_find\_cs\_node() is intended for the cases where a hkeypath is what you already have, e.g. iter() callback of cdb\_diff\_iterate(), or data provider callbacks.
