I am working on asp.net application. I have a function like this:
Public Function ExtractText(node As XmlNode) As String
End Function
I need to pass following XML as input to above function:
<myrequirements Id="7743" Type="tcg_Concept20_sc_323256419566173_context" StartNode="2724" EndNode="2869">
</myrequirements>
Then I need to get StartNode and EndNode attributes values.
Do I need to use XPath. Please suggest.
Regards,
Asif
You could try using node.Attributes("StartNode").Value and node.Attributes("EndNode").Value which will give you the values of those 2 attributes assuming the XmlNode instance passed as argument contains the XML node shown in your question.
Related
I try to pass an object to th:onclick.
When I pass a string(afficherDetails() function), everything is ok
When I pass an object(afficherDetails2() function), in the called function the object seems ok but it is empty.
function afficherDetails(employee) {
console.log("afficher Details");
document.getElementById("detailledFirstNameDataLabelId").textContent = employee.firstName;
document.getElementById("detailledLastNameDataLabelId").textContent = employee.lastName;
document.getElementById("detailledAddressDataLabelId").textContent = employee.address;
document.getElementById("detailledTitleDataLabelId").textContent = employee.title;
document.getElementById("detailledManagerDataLabelId").textContent = employee.manager;
}
function afficherDetails2(name) {
console.log("afficher Details");
document.getElementById("detailledFirstNameDataLabelId").textContent = name;
}
<td><button th:data-parameter1="${employee}" th:onclick=" afficherDetails(this.getAttribute('data-parameter1')) ">details</button></label></td>
<!--td><button th:data-parameter1="${employee.firstName}" th:onclick=" afficherDetails2(this.getAttribute('data-parameter1')) ">details</button></label></td-->
</tr>
Is it a correct behavior ? Can't we pass a complex object and we can only pass simple object?
thanks for your answer
Short answer:
You can pass a complex object to a HTML attribute - but it will be reduced to a string by the object's toString() method.
Therefore, in your case, an attempt to do the following in JavaScript...
var something = employee.firstName;
...will do nothing because the function is passed a string not an object - and therefore employee.firstName will be undefined in JavaScript.
Longer answer:
Bear in mind a couple of points:
A HTML attribute expects to contain a string:
<button th:data-parameter1="SOME VALUE IN HERE" ... >
So, the attribute data-parameter1 will be populated by Thymeleaf using a string.
All Thymeleaf processing happens on the server. Thymeleaf removes all its processing directives from the template and replaces them with valid HTML. Your JavaScript does not have access to the original Java object - just to whatever representation of that object was added to the HTML by Thymeleaf.
Let's assume you use something such as:
th:data-parameter1="${employee.firstName}"
Assuming employee.firstName evaluates to a string (John) then that is what Thymeleaf will use to produce this:
data-parameter1="John"
But if you try this:
th:data-parameter1="${employee}"
Assuming employee is your custom Java bean, then Thymeleaf will call its toString() method to use as the string.
If you have not defined a toString() method in your Employee class, then the underlying Object.toString() method will be used - and you will see something like the following - a string representation of the unique object, based on the object's name and hash code:
data-parameter1="org.yourpackage.Employee#bcb8097"
You can provide your own implementation of toString() in your Employee class to provide more useful information. But it has to be a string which can be placed in a HTML attribute.
For example, if you pass an ArrayList to the button:
List<String> names = Arrays.asList("John", "Mary");
and:
th:data-parameter1="${names}"
then your HTML button will contain this:
data-parameter1="[John, Mary]"
because [John, Mary] is the result of how ArrayList has implemented its toString() method.
You can send a certain Java objects directly to JavaScript - see JavaScript serialization. But that is probably off-topic for this question.
One extra note: In the following:
th:onclick="afficherDetails(this.getAttribute('data-parameter1'));"
You are using th:onclick - but there are no Thymeleaf expressions in the attribute, so there is nothing for Thymeleaf to process. You can just use:
onclick="afficherDetails(this.getAttribute('data-parameter1'));"
being pretty new to Spring and Jdbc, I am looking at a code from a Spring book and it is like this:
public voidaddSpitter(Spitterspitter){
jdbcTemplate.update(SQL_INSERT_SPITTER,
spitter.getUsername(),
spitter.getPassword(),
spitter.getFullName(),
spitter.getEmail(),
spitter.isUpdateByEmail());
spitter.setId(queryForIdentity());
}
Ok, so first param should be my SQL statement, but for the second param well does it get ugly in the code if there are like 15 columns in my table and I want to write 15 lines of those .get() methods? is there any nices/cleaner way of passing these?
Get the values into an Object List (in a helper method ?) and convert it into an array while passing to update().
For eg:
List<Object> insertValues = new ArrayList<Object>();
....
insertValues.add(spitter.getUsername());
....
jdbcTemplate.update(SQL_INSERT_SPITTER, insertValues.toArray());
I've faced a problem converting an Object into String in flex . my object is mydropdown selecteditem and I want to convert it to string and show it on a label tag . I've tried ".toString()" and "as String" and "String()" but none of them worked . anyone has any experience with it ?
thanks
The selectedItem object has data and label properties, depending on the data that you're supplying you'd want something like this:
mydropdown.selectedItem.label
If your "mydropdown selecteditem" is a class that you wrote yourself, you could create a toString() method in there and make it return something useful.
It's too complicate to explain but I'll give you an example
I have an AS3 ResultEvent Object
and this object has several propeties which can be accessed by this like:
event.result.name or event.result.age .....
and, I have this String variable: eventProperty:String that contains "name" or "age"
How do I access to event.result. with the variable?
thank you.
The ActionScript (or ECMAScript) . operator is just syntactic sugar, useful but not really needed. For what you want to do you can use the normal object property access operator [].
So you have to do it like this event.result[ eventProperty ].
Good luck,
Alin
You should probably move your ResultEvent object into an actual Object type first. Then you can access the properties via the object. If your object is an arraycollection, make sure to immediately pass the ResultEvent into an arraycollection since you cannot cast it as you normally might (ArrayCollection)ResultEvent. Here is how to throw the result into an object:
var yourObjectName:Object = event.result;
and here is how to throw it into an arraycollection if you need to:
var yourArrayCollection:ArrayCollection = event.result as ArrayCollection;
i get this error when i retrieve an XML that only has 1 node (no repeating nodes) and i try to store in an ArrayCollection. -When I have MORE than 1 "name" nodes...i do NOT get an error. My test show that XMLListCollection does NOT work either.
TypeError: Error #1034: Type Coercion failed: cannot convert "XXXXXX" to mx.collections.ArrayCollection.
this error occurs as the line of code:
myList= e.result.list.name;
Why can't ArrayCollection work with a single node? I'm using this ArrayCollection as a dataprovider for a Component -is there an alternative I can use that will take BOTH single and repeating nodes as well as work as a dataprovider? Thanks in advance!
code:
[Bindable]
private var myList:ArrayCollection= new ArrayCollection();
private function getList(e:Event):void{
var getStudyLoungesService:HTTPService = new HTTPService();
getStuffService.url = "website.com/asdf.php";
getStuffService.addEventListener(ResultEvent.RESULT, onGetList);
getStuffService.send();
}
private function onGetList(e:ResultEvent):void{
myList= e.result.list.name;
}
the problem here is, that if you only have one row Flex will complain since it cannot use the result as arrayCollection.
My work around was, that you put number of rows into your XML with the data you want to return:
for example I did:
<list><nr_rows>3</nr_rows><name>...</name><name>...</name><name>...</name></list>
So when I get the result back I do a check of how many rows I am getting with (You can get the number of rows returned from MySQL query with mysql_num_rows)
e.result.list.nr_rows
So if this is one, you add Object to the arrayCollection, if there are more than one you can just use result and equal it to AC (in this case projects is AC):
if (event.result.list.nr_rows == '1'){
myList.addItem(event.result.list.name);
} else {
myList = event.result.list.name;
}
If you are not particular about getting the final result onto a ArrayCollection, you can do the following to get the final result onto an XMLList or XMLListCollection.
1) set the resultFormat property of HTTPService to e4x.
2) Do not mention the root xml tag name while referencing it. Reference the output as: myList:XMLList = e.result.name.
This works whether the retrieved XML has one or more than one elements.
I just had this problem today and it led me to this question. I'm not sure if your use-case is exactly the same as mine, but you might want to try showRoot="true" on the mx:Tree. It seems to force the root node to be shown when there is only one item, and gets ignored with multiple items.
add a row object to get the row of the XML node can work, but I think some better method have be, is ActionScript is as powerful as Java?