Expand my Community achievements bar.

SOLVED

Read XML

Avatar

Level 1

Hi everybody,

I'm trying to read an xml file that I import into my form but unsuccessfully.

my xml file looks like :

<root>
    <structure code="4"/>   
    <user id="11111" lib="user1">
        <text/>
    </user >
    <user id="22222" lib="user2">
        <text/>
    </user >
    <user id="33333" lib="user3">
        <text/>
    </user >
</root>

I wanna read the id attribute of each user element.

I try using xfa.resolveNodes("$data.root.user[*]) but i can't understand how to access the value of the id attibute with teh getAttribute() method.

Any help would be appreciated.

Pasch.

1 Accepted Solution

Avatar

Correct answer by
Former Community Member

There are two things wrong.

1 - I mislead you into thinking that the attribute needed the @ in the expression. This is only used to signify that it is an attribute but is not used in the expression.

2. When you try to use the i counter in an expression you must use the xfa.resolveNode syntax. This takes a string as an expression and you can inmcorporate your counter into the epression. The way you are doing it the counter never gets resolved before trying to interpret the expression. So the code should look like this:

var

objDataGroup = xfa.resolveNodes("$data.root.user[*]");

var

length1= objDataGroup.length;

for

(var i=0; i<length1;i++){

     var code = xfa.resolveNode("xfa.datasets.data.root.user[" + i + "].id").value;

     xfa.host.messageBox("code="

+ code);

}

View solution in original post

4 Replies

Avatar

Former Community Member

The attributes get turned into nodes in the Data Dom prefixed by an @. To see what the structure is put a large multiline field on the form called TextField1. Add a regular button onto the form. On the click event add this code:

TextField1.rawValue = xfa.datasets.data.saveXML("pretty")

Now when you get the node that you want you yuou can get its value bu using:

xfa.datasets.data.root.userid[occuarance number].@lib.value

Paul

Avatar

Level 1

Thanks Paul for your answer,

I tried it but it still doesn't work on my form.

I'll try to explain my problem other way.

I work with an XSD schema as data connection and I've already bind all the fields.

then,  I've got a button ( xfa.host.importData() ) on my form which load an xml file to populate the different fields of the form.

I need to retrieve the value of an attribute (the id attribute of the user element as shown in the xml example in my last post).

here's my code (on the form:ready event):

var objDataGroup =xfa.resolveNodes("$data.root.user[*]");
var length1= objDataGroup.length;

for(var i=0; i<length1;i++){
     var code =  xfa.datasets.data.root.user["+i+"].@id.value);

     xfa.host.messageBox("code="+code);

}

Unfortunately the message box never appears. My code should be wrong but I don't know why...

thanks for your help.

Pasch

Avatar

Correct answer by
Former Community Member

There are two things wrong.

1 - I mislead you into thinking that the attribute needed the @ in the expression. This is only used to signify that it is an attribute but is not used in the expression.

2. When you try to use the i counter in an expression you must use the xfa.resolveNode syntax. This takes a string as an expression and you can inmcorporate your counter into the epression. The way you are doing it the counter never gets resolved before trying to interpret the expression. So the code should look like this:

var

objDataGroup = xfa.resolveNodes("$data.root.user[*]");

var

length1= objDataGroup.length;

for

(var i=0; i<length1;i++){

     var code = xfa.resolveNode("xfa.datasets.data.root.user[" + i + "].id").value;

     xfa.host.messageBox("code="

+ code);

}

Avatar

Level 1

Paul,

ok, that works.

thanks a lot for your help.

Pasch.

The following has evaluated to null or missing: ==> liqladmin("SELECT id, value FROM metrics WHERE id = 'net_accepted_solutions' and user.id = '${acceptedAnswer.author.id}'").data.items [in template "analytics-container" at line 83, column 41] ---- Tip: It's the step after the last dot that caused this error, not those before it. ---- Tip: If the failing expression is known to be legally refer to something that's sometimes null or missing, either specify a default value like myOptionalVar!myDefault, or use <#if myOptionalVar??>when-present<#else>when-missing. (These only cover the last step of the expression; to cover the whole expression, use parenthesis: (myOptionalVar.foo)!myDefault, (myOptionalVar.foo)?? ---- ---- FTL stack trace ("~" means nesting-related): - Failed at: #assign answerAuthorNetSolutions = li... [in template "analytics-container" at line 83, column 5] ----