Retrive XML data from CI's in code

English! place to talk about development, programming and coding
Post Reply
Basia
Znuny newbie
Posts: 7
Joined: 16 Jan 2015, 10:30
Znuny Version: 3.3.10
Real Name: Seba

Retrive XML data from CI's in code

Post by Basia »

i have some trouble reading data from the CI's, i am trying to read the XML
data of the latest version of a CI.


this is code i have managed so far:

Code: Select all

my $ConfigItem =  $CommonObject{ConfigItemObject}->ConfigItemGet(
    ConfigItemID => $ConfigItemID,
    );


my $Version = $CommonObject{ConfigItemObject}->VersionGet(
    VersionID  => $ConfigItem->{LastVersionID},
    XMLDataGet => 1,
    );

my $XMLArray = $Version->{_XMLVersionGet};

print $XMLArray->{Name};

but i am not sure how to retrieve the data from $XMLArray.
it says it is uninitialized.

How do i read data of one (or more) CI's?
i am interested in data like: vendor, owner, name etc.
RStraub
Znuny guru
Posts: 2210
Joined: 13 Mar 2014, 09:16
Znuny Version: 6.0.14
Real Name: Rolf Straub

Re: Retrive XML data from CI's in code

Post by RStraub »

Code: Select all

my $Version = $CommonObject{ConfigItemObject}->VersionGet(
    VersionID  => $ConfigItem->{LastVersionID},
    XMLDataGet => 1,
    );
This is allright, you now have a reference to the latest version.
Access it's XML data via:

Code: Select all

$Version->{XMLData}
Either print or dumper. I tried to get a certain value of that data, but our CI definition was huge, I got confused and gave up ;)
Currently using: OTRS 6.0.14 -- MariaDB -- Ubuntu 16 LTS
Basia
Znuny newbie
Posts: 7
Joined: 16 Jan 2015, 10:30
Znuny Version: 3.3.10
Real Name: Seba

Re: Retrive XML data from CI's in code

Post by Basia »

can you help me with this then? i am trying to print it, either as a whole,
or in a for loop. all i get is references? how do i make it output the data?

Code: Select all

print "\nXMLDATA:  $Version->{XMLData} \n";

for ($Version->{XMLData}){
        print $_;
}
this is the output:

Code: Select all

XMLDATA:  ARRAY(0x3eaf7e8) 
ARRAY(0x3eaf7e8)
RStraub
Znuny guru
Posts: 2210
Joined: 13 Mar 2014, 09:16
Znuny Version: 6.0.14
Real Name: Rolf Straub

Re: Retrive XML data from CI's in code

Post by RStraub »

Yeah, that's a nasty stack of hash-ref and array-refs.

Say you have a field called "InventoryNumber" you want to read. Then you probably get the value with this:

Code: Select all

print "$Version->{XMLData}[1]->{Version}[1]->{InventoryNumber}[1]->{Content}"
Currently using: OTRS 6.0.14 -- MariaDB -- Ubuntu 16 LTS
Basia
Znuny newbie
Posts: 7
Joined: 16 Jan 2015, 10:30
Znuny Version: 3.3.10
Real Name: Seba

Re: Retrive XML data from CI's in code

Post by Basia »

can you explain your code a bit more?

i am wondering specifically why the need for

Code: Select all

[1]
is this some array thing?

and about the

Code: Select all

{version}
is this version of Version? how can i understand this?
reneeb
Znuny guru
Posts: 5018
Joined: 13 Mar 2011, 09:54
Znuny Version: 6.0.x
Real Name: Renée Bäcker
Company: Perl-Services.de
Contact:

Re: Retrive XML data from CI's in code

Post by reneeb »

[1] accesses the second element of the array(reference). The first element is always undefined (don't know why OTRS does this).

"Version" is a fixed string...
Perl / Znuny development: http://perl-services.de
Free Znuny add ons from the community: http://opar.perl-services.de
Commercial add ons: http://feature-addons.de
Basia
Znuny newbie
Posts: 7
Joined: 16 Jan 2015, 10:30
Znuny Version: 3.3.10
Real Name: Seba

Re: Retrive XML data from CI's in code

Post by Basia »

i think i can retrieve most of the items (i have tested most) but i am having trouble with 'ExpirationDate'.

i try to recieve it with:

Code: Select all

print "Expiration Date : " . $Version->{XMLData}[1]->{Version}[1]->{ExpirationDate}[1]->{Content};
but the output says uninitialized:

Code: Select all

Use of uninitialized value in concatenation (.) or string at ./otrs.wicked.pl line 77
am i doing something wrong with this?
reneeb
Znuny guru
Posts: 5018
Joined: 13 Mar 2011, 09:54
Znuny Version: 6.0.x
Real Name: Renée Bäcker
Company: Perl-Services.de
Contact:

Re: Retrive XML data from CI's in code

Post by reneeb »

Dumper was already mentioned. That should show you the underlying datastructure...
Perl / Znuny development: http://perl-services.de
Free Znuny add ons from the community: http://opar.perl-services.de
Commercial add ons: http://feature-addons.de
RStraub
Znuny guru
Posts: 2210
Joined: 13 Mar 2014, 09:16
Znuny Version: 6.0.14
Real Name: Rolf Straub

Re: Retrive XML data from CI's in code

Post by RStraub »

Could you post the definition of your CI ?
Currently using: OTRS 6.0.14 -- MariaDB -- Ubuntu 16 LTS
RStraub
Znuny guru
Posts: 2210
Joined: 13 Mar 2014, 09:16
Znuny Version: 6.0.14
Real Name: Rolf Straub

Re: Retrive XML data from CI's in code

Post by RStraub »

reneeb wrote:[1] accesses the second element of the array(reference). The first element is always undefined (don't know why OTRS does this).

"Version" is a fixed string...
Oh yeah - that bugged me quiete a while. As I don't have much experience with perl, I took me hours to work through the datastructure (as I wanted to access the content of a "sub" definition).
Currently using: OTRS 6.0.14 -- MariaDB -- Ubuntu 16 LTS
Post Reply