Tags |
![]() |
Aug 25 2008, 11:05 PM
Post
#1
|
|||
|
Very Active Member Posts: 163 Joined: 14-June 05 From: Seattle, WA Member No.: 2411 Using LabVIEW Since:1993 LV:8.5.1 ,8.20 ,7.1.1
|
Here is an example of how to read an XML file, extract attributes of elements and also validate it against a schema.
I use this to read user-editable configuration files for my project. By using XML with a schema, I can control the format of the file and verify that it adheres to this format when reading it. This allows me to abort application startup with a meaningful error message if someone makes an invalid edit to the configuration file. This is also a good primer on how to read XML files using .NET calls. I hope you find this useful. I also suggest you read some of the tutorials on the net about XML and XSD (schema) to understand how to create and edit your own files. In addition, I recommend XMLSpy by Altova for working with XML and XSD files. -John
DEMO_XML_File_Read.zip ( 136.39K )
Number of downloads: 156-------------------- ---------
You mean you still use a keyboard to write your code? How quaint...
|
||
|
|
|||
| Ad |
Aug 25 2008, 11:05 PM
Post
#
|
||
|
|
|
||
|
|
|||
Aug 26 2008, 12:37 PM
Post
#2
|
|||
|
More Active Member Posts: 48 Joined: 11-June 08 Member No.: 11742 Using LabVIEW Since:1997 LV:8.6 ,. ,.
|
Useful demo. My only suggestion would be to stay away from the XML file specifying the schema file. I'd specify it to the validator explicitly, otherwise validating against a possibly user modified/specified schema isn't of much use. Ideally in an application implementation, the schema would be an internal resource, or a checksumed external file so it's integrity can be verified, but that last step is a bit beyond the scope of a demo. A demo simply taking the path to the schema would suffice.
I *really* like what you did with the error queue during validation. That's bloody genius, I'm gonna have to remember that trick.
|
||
|
|
|||
Aug 26 2008, 03:48 PM
Post
#3
|
|||
|
Very Active Member Posts: 163 Joined: 14-June 05 From: Seattle, WA Member No.: 2411 Using LabVIEW Since:1993 LV:8.5.1 ,8.20 ,7.1.1
|
My only suggestion would be to stay away from the XML file specifying the schema file. Yes, that is an option, but then they would have to select the schema file or I would have to hard code it's name into the code. Also, if I choose to put the schema in another directory, I would have to hardcode that full path into the code. If I left the schema in the same directory and used the reletive path of the XML doc, then there is nothing that would stop them from seeing it and editing it as well. Finally, by having the schema specified in the XML and having it local to the XML, other XML editors can use this to enforce the schema when editing the file. This helps avoid errors when making edits. My goal is to help the user not make a mistake when editing that would cause the file to be unreadable. If they wish to be malicious, then I really don't care. But if someone sees value in having the schema specified externally, that can be done. As one of my old professors used to say: 'That exercise is left for the student'. BTW: I'm glad you like the callback trick! I'l have to post a few more examples of interesting 'tricks' I have come across over the years... -John -------------------- ---------
You mean you still use a keyboard to write your code? How quaint...
|
||
|
|
|||
Aug 28 2008, 12:26 PM
Post
#4
|
|||
|
Active Member Posts: 17 Joined: 9-June 08 Member No.: 11721 Using LabVIEW Since:2008 LV:8.5.1 ,. ,.
|
Hi there,
Thanks for the demo/tutorial I am an RF engineer who is now in a software role as a LabVIEW programmer and I have found learning to use XML really difficult as all the examples are are in code like javascript or c# etc. All my colleagues have never seen LabVIEW and I dont really know with any ability any other programming languages so there is a massive barrier to learning to use XML and .NET calls. First I'll give a brief description of my task and then tell you my problems. I have to program a system for making some RF tests on a satellite payload. My system will be used for some of the smaller tests and is based on Off the shelf Agilent RF equipment. This will be part of a larger system that commands the payload configuration, stores data, graphically shows the payload configuration and monitors the payoad temp/volts/amps etc. These parts are done using teststand, SQL, in-house created software and webservices. This is the way it has to be done since the flexibilty to change supplier of the Main RF Measurement System is needed for future flexibility. What my specific problem is that I am subscribing to the webservice and from this service I am given a string XML which I have just about managed to read and query using similar methods from your demo. Your demo uses single elements with lots of attributes for data this make it easy to iterate through and grab out an array and is ideal for your suggested gonfiguration use. However, the format of the parameters passed in the webservice conforms more strictly to the 'best form' w3c standard which moves away from attribute use and adds them as sub elements. Are you familiar with parsing this type of XML. As I am quite fresh to the XML scene do you consider it to be very/too difficult to iterate the elements instead. My biggest problem with XML something you may be able to help/make clearer for me is it seems like you can only really rip out data/info to a table or something structured if you know what it is you are going to receive. I am wondering if this is where the schema should be used or if there are better more generic element searches that can be used. I have had some success using the wildcard functions with in the xpath.selectnodes function to get the info but due to the varying attributes and elements sizes it is extremely tricky to align the data correctly in tables. Here is an excerpt from the xml from altova:- <Parameter xmlns="" Name="MaskSet1" Type="Mask"> <ARRAY> <ARRAY xmlns="" Name="MaskXcoord" Type="FloatArray"> <VALUE Col="1">-2.000000000000000e+001</VALUE> <VALUE Col="2">-1.500000000000000e+001</VALUE> <VALUE Col="3">1.500000000000000e+001</VALUE> <VALUE Col="4">2.000000000000000e+001</VALUE> </ARRAY> <ARRAY xmlns="" Name="MaskYcoord" Type="FloatArray"> <VALUE Col="1">-3.000000000000000e+001</VALUE> <VALUE Col="4">-3.000000000000000e+001</VALUE> <VALUE Col="2">-2.500000000000000e+001</VALUE> <VALUE Col="3">-2.500000000000000e+001</VALUE> </ARRAY> <ARRAY xmlns="" Name="MaskId" Type="IntegerArray"> <VALUE Col="1">1</VALUE> <VALUE Col="2">1</VALUE> <VALUE Col="3">2</VALUE> <VALUE Col="4">2</VALUE> </ARRAY> </ARRAY> </Parameter> Altova recognises the nesting correctly but the system xml amd xpath functions don't seem to recognise child and parent elements correctly. Any help would be greatly appreciated and thanks in advance.
|
||
|
|
|||
Aug 28 2008, 08:04 PM
Post
#5
|
|||
|
Very Active Member Posts: 163 Joined: 14-June 05 From: Seattle, WA Member No.: 2411 Using LabVIEW Since:1993 LV:8.5.1 ,8.20 ,7.1.1
|
I'm not completely sure I understand all your questions, but if you are asking if this format can be read, the answer is yes. However, I have found that in LabVIEW, you cannot make a generic XML reader easily. In most cases, you need to know the format of the XML document when you write the LabVIEW code to parse it.
That does not mean that you cannot have variable length sections, like you excerpt shows. You can use the features in the MSXML.NET assembly to determine child count and iterate through the elements, building an array in LV. This is something I do all the time. I have a much more complex XML file that describes an entire test hierarchy that has N test plans with N test suites, each with N tests, each with N parameters and N measurements. I am able to read this into a complex array of clusters of arrays of clusters, etc... So, it can be done, but you need to think carefully about the structure of the XML you will receive and then design you code to deal with the sections that are variable in length. I also noted that your example stored the data in elements but still used attributes to specify information about each element. So, you will need to deal with extracting that part of the data and use it to organize the elements in their proper order, unless you can assume they will always be formatted in ascending numerical order (which is likely but nothing is ever guaranteed). Anyways, I think the example code gives you a good start toward reading this but I suspect you will need to modify it to fit your format. My goal was not to make a generic XML reader but rather demo the basics of accessing XML data and protecting it with a schema. You might also check out JKI's EasyXML tools. I think they might be useful to help parse your XML. -------------------- ---------
You mean you still use a keyboard to write your code? How quaint...
|
||
|
|
|||
Aug 28 2008, 09:44 PM
Post
#6
|
||||
![]() Extremely Active JKI ![]() Posts: 612 Joined: 19-June 03 From: Bay Area, CA (USA) Member No.: 121 Using LabVIEW Since:1998 LV:8.5.1 ,8.6 ,8.2.1
My Blog
|
Hi there, NeilA,Thanks for the demo/tutorial I am an RF engineer who is now in a software role as a LabVIEW programmer and I have found learning to use XML really difficult as all the examples are are in code like javascript or c# etc. All my colleagues have never seen LabVIEW and I dont really know with any ability any other programming languages so there is a massive barrier to learning to use XML and .NET calls. First I'll give a brief description of my task and then tell you my problems. I have to program a system for making some RF tests on a satellite payload. My system will be used for some of the smaller tests and is based on Off the shelf Agilent RF equipment. This will be part of a larger system that commands the payload configuration, stores data, graphically shows the payload configuration and monitors the payoad temp/volts/amps etc. These parts are done using teststand, SQL, in-house created software and webservices. This is the way it has to be done since the flexibilty to change supplier of the Main RF Measurement System is needed for future flexibility. What my specific problem is that I am subscribing to the webservice and from this service I am given a string XML which I have just about managed to read and query using similar methods from your demo. Your demo uses single elements with lots of attributes for data this make it easy to iterate through and grab out an array and is ideal for your suggested gonfiguration use. However, the format of the parameters passed in the webservice conforms more strictly to the 'best form' w3c standard which moves away from attribute use and adds them as sub elements. Are you familiar with parsing this type of XML. As I am quite fresh to the XML scene do you consider it to be very/too difficult to iterate the elements instead. My biggest problem with XML something you may be able to help/make clearer for me is it seems like you can only really rip out data/info to a table or something structured if you know what it is you are going to receive. I am wondering if this is where the schema should be used or if there are better more generic element searches that can be used. I have had some success using the wildcard functions with in the xpath.selectnodes function to get the info but due to the varying attributes and elements sizes it is extremely tricky to align the data correctly in tables. Here is an excerpt from the xml from altova:- <Parameter xmlns="" Name="MaskSet1" Type="Mask"> <ARRAY> <ARRAY xmlns="" Name="MaskXcoord" Type="FloatArray"> <VALUE Col="1">-2.000000000000000e+001</VALUE> <VALUE Col="2">-1.500000000000000e+001</VALUE> <VALUE Col="3">1.500000000000000e+001</VALUE> <VALUE Col="4">2.000000000000000e+001</VALUE> </ARRAY> <ARRAY xmlns="" Name="MaskYcoord" Type="FloatArray"> <VALUE Col="1">-3.000000000000000e+001</VALUE> <VALUE Col="4">-3.000000000000000e+001</VALUE> <VALUE Col="2">-2.500000000000000e+001</VALUE> <VALUE Col="3">-2.500000000000000e+001</VALUE> </ARRAY> <ARRAY xmlns="" Name="MaskId" Type="IntegerArray"> <VALUE Col="1">1</VALUE> <VALUE Col="2">1</VALUE> <VALUE Col="3">2</VALUE> <VALUE Col="4">2</VALUE> </ARRAY> </ARRAY> </Parameter> Altova recognises the nesting correctly but the system xml amd xpath functions don't seem to recognise child and parent elements correctly. Any help would be greatly appreciated and thanks in advance. As John mentioned, you could try the EasyXML toolkit. I just spent a few minutes to create a LabVIEW Data structure to accommodate your XML data (to read it in LabVIEW) and it work fine (see screenshot below). PJM --------------------
|
|||
|
|
||||
Aug 28 2008, 10:01 PM
Post
#7
|
|||
|
Extremely Active Member Posts: 378 Joined: 26-March 06 From: Victoria, BC Canada Member No.: 4568 Using LabVIEW Since:1998 LV:8.6 ,8.5.1 ,8.2.1
|
Here is an example of how to read an XML file, extract attributes of elements and also validate it against a schema. I use this to read user-editable configuration files for my project. By using XML with a schema, I can control the format of the file and verify that it adheres to this format when reading it. This allows me to abort application startup with a meaningful error message if someone makes an invalid edit to the configuration file. This is also a good primer on how to read XML files using .NET calls. I hope you find this useful. I also suggest you read some of the tutorials on the net about XML and XSD (schema) to understand how to create and edit your own files. In addition, I recommend XMLSpy by Altova for working with XML and XSD files. -John
DEMO_XML_File_Read.zip ( 136.39K )
Number of downloads: 156Does this make use of any OpenG code or is it all native LV? -------------------- The power of NeuroCARETM
|
||
|
|
|||
Aug 28 2008, 10:32 PM
Post
#8
|
|||||
|
Very Active JKI ![]() Posts: 145 Joined: 3-August 06 From: San Francisco, CA Member No.: 5746 Using LabVIEW Since:1996 LV:8.20 ,. ,.
|
You might also check out JKI's EasyXML tools. I think they might be useful to help parse your XML. JKI has a really easy to use toolkit called the JKI EasyXML Toolkit. Its designed such that all you have to do is to define a cluster that matches your XML structure and you're basically good to go. You can find out more on www.jkisoft.com/easyxml. Here are screenshots of how the data would be parsed using our tool, I've also attached the VI that was used in the screenshots. [Front Panel] [Block Diagram] In order for the attached VI to run, you'll need to install the evaluation (or professional) copy of EasyXML. You can get it from the jkisoft website here: http://www.jkisoft.com/easyxml/download/ Go to jkisoft.com if you have additional questions on our solution. [UPDATE] Apparently Philippe posted at the same time I did, but luckily our solutions are the same Does this make use of any OpenG code or is it all native LV? I'm not sure what 'this' means... I assume you mean the JKI EasyXML Toolkit because obviously the NI code does not depend on OpenG (and likely never will). The JKI EasyXML toolkit depends on the following OpenG packages: oglib_string oglib_lvdata oglib_error oglib_array
Attached File(s)
-------------------- got VIPM?
|
||||
|
|