Support Portal

for ProcessRobot and WinAutomation

Start a new topic
Answered

How to get Value of XML Element

Hello,


a have an issue with reading out elements of a XML File. I have a UTF-8 XML File. The "Read XML File" Action works perfect. But how do I get the values out of it, like the TaxNumber? I uploaded some pictures.

And what should I do if i have many elements in the XML-file with the same name?

Thanks for your help!


image

image


image




Best Answer

Hello Konstantin,


In order to retrieve all "TaxNumber" elements you may use the "Execute XPath Expression" action with the XPATH Query: //TaxNumber.

This way you will get all the elements with name "TaxNumber" regardless of their position

For example if <TaxNumber>01055601</TaxNumber> and <TaxNumber>01055330</TaxNumber> are retrieved in a list. You may afterwards, use a "Parse Text" action and this regular expression: (?<=<.*>).*(?=</.*>)


If you wish to retrieve an XML element's attribute's value for example the "female" part of the element <person gender="female"> you can use the "Get XML Element Attribute" action.


Finally, in case you have many elements in the XML-file with the same name and wish to retrieve only specific element you may execute a query with specifying the exact path in which to look for this specific element.


For example, if you want to retrieve the "TaxNumber" element located under the "Exporter" parent element you use this XPath Query: //Exporter/TaxNumber


For more information and examples regarding XML and XPath you can read these websites:  https://www.w3schools.com/xml/xml_xpath.asp  and  https://www.w3schools.com/xml/default.asp 


In addition, when creating XPath queries you could also test them using this online tool: https://www.freeformatter.com/xpath-tester.html 


ADMIN
Answer

Hello Konstantin,


In order to retrieve all "TaxNumber" elements you may use the "Execute XPath Expression" action with the XPATH Query: //TaxNumber.

This way you will get all the elements with name "TaxNumber" regardless of their position

For example if <TaxNumber>01055601</TaxNumber> and <TaxNumber>01055330</TaxNumber> are retrieved in a list. You may afterwards, use a "Parse Text" action and this regular expression: (?<=<.*>).*(?=</.*>)


If you wish to retrieve an XML element's attribute's value for example the "female" part of the element <person gender="female"> you can use the "Get XML Element Attribute" action.


Finally, in case you have many elements in the XML-file with the same name and wish to retrieve only specific element you may execute a query with specifying the exact path in which to look for this specific element.


For example, if you want to retrieve the "TaxNumber" element located under the "Exporter" parent element you use this XPath Query: //Exporter/TaxNumber


For more information and examples regarding XML and XPath you can read these websites:  https://www.w3schools.com/xml/xml_xpath.asp  and  https://www.w3schools.com/xml/default.asp 


In addition, when creating XPath queries you could also test them using this online tool: https://www.freeformatter.com/xpath-tester.html 


1 person likes this

Hello Michaelis,


thanks for your very detailed answer and help. Unfortunatlly I try your different solutions but I don't get any values. So i try the "Execute XPath Expression" because i think this is the right action for me. When I look in the variables pane, the list of "TaxNumbers" is empty.

The same happens when i type in the exact Xpath Query of TaxNumbers or any other <Attribute>.

So maybe it has to do something with the XML-File? It is a "Namespace" XML-File, so maybe i have to use other commands to access the node with my TaxNumber Value? Or do you have any other idea?

I attached some more pictures.

Thanks again for you help and have nice evening.

Greets,

Konstantin

image

image


 

image



Hi again,


I also test it with your free online XPather Tester: https://www.freeformatter.com/xpath-tester.html#ad-output

And here i got a solution for this Xpath:

/CustomsExportDeclaration/DocumentControl/Exporter/TaxNumber

The Result is:

Element='<TaxNumber xmlns="http://ebxml.customs.go.th/XMLSchema/CustomsExportDeclaration_4_00">0105560165611</TaxNumber>'

But when I do the same in WinAutomation, the values are empty... I don't get it.

Thanks a lot.


Greets,

Konstantin


 

ADMIN

Hi Konstantin,


You could try removing the namespace attribute ( xmlns="http://ebxml.customs.go.th/XMLSchema/CustomsExportDeclaration_4_00" ) from the CustomsExportDeclaration element and try again as sometimes namespaces can break XPATH. There have been also some reports on the internet regarding similar issues (such as https://stackoverflow.com/questions/5239685/xml-namespace-breaking-my-xpath). 

Hope this helps.


1 person likes this

Hello Michalis,


thanks again for your quick response. So yes, if I change the namespace attribute, I have no problems with reading out the values.

So I get every day 100 XML-files. Before reading them in with the Robot, I have to do a file-manipulation. So before execute the regular process of reading out the values, I can develop a robot to delete this namespace line xmlns="........" and save the file with a new name.

I also think this is the only practicable solution for my issue.


ADMIN

Hello Konstantin,


What you are describing could be a possible way to handle XML files with namespaces when using XPath. Another possible way would be to try using XPath queries with the following format:

for example:

//*[local-name()='abc']/*[local-name()='def']/*[local-name()='ghi'] (where 'abc, 'def', 'ghi' are the XML elements/nodes of your query)

For example, //*[local-name()='TaxNumber'] this returns all two "TaxNumber" elements regardless of their relative path.

You may also find some more information in this link: How does XPath deal with XML namespaces?


1 person likes this
Login or Signup to post a comment