Unofficial Programming Thread III

Post » Tue Aug 03, 2010 6:35 am

Previous threads:
http://www.gamesas.com/index.php?/topic/1092028-unofficial-programming-thread/page__p__15933926__fromsearch__1&#entry15933926
http://www.gamesas.com/index.php?/topic/1092028-unofficial-programming-thread/page__p__15933926__fromsearch__1&#entry15933926

Most recent posts:
I need some help reading XML files in C#. Here's what's in the XML file.
        An example assignment XML file        3        2/7/2011        3/1/2011


Wow. You can't even read that.

How about this?


Here is a small sample program to help get you going in the right direction.
using System;using System.Xml.Linq;namespace XMLReader{    class Program    {        static void Main()        {            XDocument Assignments = XDocument.Load("assignments.xml");            XElement root = Assignments.Root;            if (root != null)            {                Console.WriteLine("Assignment Name: " + root.Attribute("name").Value);                Console.WriteLine("Description: " + root.Element("Description").Value);                Console.WriteLine("Importance: " + root.Element("Importance").Value);                Console.WriteLine("Assign Date: " + root.Element("AssignDate").Value);                Console.WriteLine("Due Date: " + root.Element("DueDate").Value);            }            Console.ReadLine();        }    }}

User avatar
CORY
 
Posts: 3335
Joined: Sat Oct 13, 2007 9:54 pm

Post » Tue Aug 03, 2010 3:35 am

Thanks for the help. I got it to work.

Does anyone else use the gtk+ libraries for gui designing? I hate them. Overly complicated.
User avatar
Stephanie Kemp
 
Posts: 3329
Joined: Sun Jun 25, 2006 12:39 am

Post » Tue Aug 03, 2010 12:20 am

Thanks for the help. I got it to work.

Does anyone else use the gtk+ libraries for gui designing? I hate them. Overly complicated.

I haven't but I should look into it. I am going to start an XML processing class library for myself soon.

Anyone know how diffuclt it is to switch from Subversion to Mercurial with Google Code? Or even if Mercurial allows for local repositories.
User avatar
Honey Suckle
 
Posts: 3425
Joined: Wed Sep 27, 2006 4:22 pm

Post » Mon Aug 02, 2010 10:08 pm

I don't know how much you've studied XML, but there are generally two ways of doing XML processing - DOM and SAX. The former first forms a document model of the whole xml file in memory and you can do queries on the structure directly. Look for "xpath" queries. It's ok for small files, but takes a whole lot of memory. You can even do namespaces and schema validations, but maybe that's going too far.

The latter parses the file line by line. This is quick and takes only a little memory, but you may need to build your own structures to hold the data in anyways if you need to keep track of what you just read. That's not done by the class library. The implementations I've seen are based on event handling where the class library calls certain functions/events whenever it encounters an element, attribute, processing instructions, begin and end document, etc, while reading the file. You can reimplement these functions separately for each format you are reading. You also need to catch errors the library might throw in case of error. In code you just start the process and wait until it finishes.

My question is, is LINQ comparable to or the same as DOM? Is it another Microsoft redesign of an old standard, or where did this LINQ come from? Is it specific to .NET?
User avatar
Lewis Morel
 
Posts: 3431
Joined: Thu Aug 16, 2007 7:40 pm

Post » Tue Aug 03, 2010 7:05 am

I don't know how much you've studied XML, but there are generally two ways of doing XML processing - DOM and SAX. The former first forms a document model of the whole xml file in memory and you can do queries on the structure directly. Look for "xpath" queries. It's ok for small files, but takes a whole lot of memory. You can even do namespaces and schema validations, but maybe that's going too far.

The latter parses the file line by line. This is quick and takes only a little memory, but you may need to build your own structures to hold the data in anyways if you need to keep track of what you just read. That's not done by the class library. The implementations I've seen are based on event handling where the class library calls certain functions/events whenever it encounters an element, attribute, processing instructions, begin and end document, etc, while reading the file. You can reimplement these functions separately for each format you are reading. You also need to catch errors the library might throw in case of error. In code you just start the process and wait until it finishes.

Very interesting. Thanks for the info. :D
User avatar
teeny
 
Posts: 3423
Joined: Sun Feb 25, 2007 1:51 am

Post » Mon Aug 02, 2010 7:10 pm

I don't know how much you've studied XML, but there are generally two ways of doing XML processing - DOM and SAX. The former first forms a document model of the whole xml file in memory and you can do queries on the structure directly. Look for "xpath" queries. It's ok for small files, but takes a whole lot of memory. You can even do namespaces and schema validations, but maybe that's going too far.

The latter parses the file line by line. This is quick and takes only a little memory, but you may need to build your own structures to hold the data in anyways if you need to keep track of what you just read. That's not done by the class library. The implementations I've seen are based on event handling where the class library calls certain functions/events whenever it encounters an element, attribute, processing instructions, begin and end document, etc, while reading the file. You can reimplement these functions separately for each format you are reading. You also need to catch errors the library might throw in case of error. In code you just start the process and wait until it finishes.


It's a nitpick, but SAX generally reads the file character by character (with buffering), not line by line, since large XML documents often do not contain line breaks at all and in any case, they do not affect the format of XML. SAX is generally more of a pain to deal with, but in some cases it's a lifesaver - try parsing the Wikipedia dump with DOM.

My question is, is LINQ comparable to or the same as DOM? Is it another Microsoft redesign of an old standard, or where did this LINQ come from? Is it specific to .NET?

It's not really comparable. LINQ is just a unified interface for performing SQL-like queries that can be shared between ordinary in-memory data structures (compiled to a sequence of method calls), SQL databases (compiled to SQL) and XML files (converted to calls to a special XML query engine). And yes, it's specific to .NET.
User avatar
Mashystar
 
Posts: 3460
Joined: Mon Jul 16, 2007 6:35 am

Post » Tue Aug 03, 2010 5:45 am

Other than the W3Schools links does anyone have any good links for defining my own schema? I want to allow XML files to be used with a program I wrote and I will allow people to write it themselves but I want to be sure if conforms to how my program will handle it such as element names.
User avatar
Katie Pollard
 
Posts: 3460
Joined: Thu Nov 09, 2006 11:23 pm

Post » Tue Aug 03, 2010 7:03 am

Does anybody have any recommendations for Java textbooks? I was planning on buying an older used textbook. I don't really want to spend over $30 if possible.

I would like a book that's easy on the reader (visually wise). If possible I'd like to avoid black & white walls of text.

Any recommendations are appreciated :)

A few I was looking at:
http://www.amazon.com/Java-Beginners-Guide-4th-Ed/dp/0072263849/ref=sr_1_10?s=books&ie=UTF8&qid=1297822957&sr=1-10
http://www.amazon.com/Effective-Java-2nd-Joshua-Bloch/dp/0321356683/ref=sr_1_1?s=books&ie=UTF8&qid=1297826747&sr=1-1
http://www.amazon.com/Learning-Java-Patrick-Niemeyer/dp/0596008732/ref=sr_1_3?s=books&ie=UTF8&qid=1297826747&sr=1-3
http://www.amazon.com/Murachs-Java-SE-Training-Reference/dp/1890774421/ref=sr_1_7?s=books&ie=UTF8&qid=1297826702&sr=1-7
User avatar
Mark
 
Posts: 3341
Joined: Wed May 23, 2007 11:59 am

Post » Tue Aug 03, 2010 8:58 am

Does anybody have any recommendations for Java textbooks? I was planning on buying an older used textbook. I don't really want to spend over $30 if possible.

I would like a book that's easy on the reader (visually wise). If possible I'd like to avoid black & white walls of text.

Any recommendations are appreciated :)

A few I was looking at:
http://www.amazon.com/Java-Beginners-Guide-4th-Ed/dp/0072263849/ref=sr_1_10?s=books&ie=UTF8&qid=1297822957&sr=1-10
http://www.amazon.com/Effective-Java-2nd-Joshua-Bloch/dp/0321356683/ref=sr_1_1?s=books&ie=UTF8&qid=1297826747&sr=1-1
http://www.amazon.com/Learning-Java-Patrick-Niemeyer/dp/0596008732/ref=sr_1_3?s=books&ie=UTF8&qid=1297826747&sr=1-3
http://www.amazon.com/Murachs-Java-SE-Training-Reference/dp/1890774421/ref=sr_1_7?s=books&ie=UTF8&qid=1297826702&sr=1-7

You might be ab;e to find free online ebooks if those will work for you. Not sure which physical books will be good but if you find any by Apress that is what I used for C# and WPF and they seem decent. Wrox textbooks usually have problems they want you to work through at the end of each chapter so that would be another possibility.
User avatar
kiss my weasel
 
Posts: 3221
Joined: Tue Feb 20, 2007 9:08 am

Post » Mon Aug 02, 2010 8:24 pm

It's a nitpick, but SAX generally reads the file character by character (with buffering), not line by line, since large XML documents often do not contain line breaks at all and in any case, they do not affect the format of XML. SAX is generally more of a pain to deal with, but in some cases it's a lifesaver - try parsing the Wikipedia dump with DOM.

It's not really comparable. LINQ is just a unified interface for performing SQL-like queries that can be shared between ordinary in-memory data structures (compiled to a sequence of method calls), SQL databases (compiled to SQL) and XML files (converted to calls to a special XML query engine). And yes, it's specific to .NET.

Ah, that's true and thanks for the info.. I'm using tools that "pretty-print" the XML for display so I didn't even think about it. I've had to deal with imports of XML messages that are hundreds of megabytes is size. The event handling I implemented on the import project were mostly on element by element level. There are separate events for begin and end element, but there is not even an event for each line read, I don't think.

DOM is out of the question with files that big at least if the import process had performance or memory consumption requirements. While handling 500MB XML files the little servers could run out of memory, even though you could technically split the file in smaller chunks to begin with so that even DOM could still handle it. However, the queries tend to take exponentially longer times with growing file size. Don't use double slash "//" in the query because then it would go through every single node in the document model looking for hits, for example.

The good thing with DOM is that you can do complex queries and evaluations on the data, and stylesheets that use those queries are pretty neat for example in transforming the xml into readable reports in html. It's possible to do XML to PDF conversion for printouts with the help of xsl:fo, which is a sort of extension to xsl stylesheets with a separate tool that does the final pdf conversion.

Since it's possible to do so many conversions from XML, I'd like to see an implementation of a stylesheet that converts some XML directly to Excel and back. That must have been done already by now, correct? Since the Excel file format technically already is in some kind of XML, or at least is based on XML from what I hear, the conversion should be easy, or is it? Is it possible to use a stylesheet in this way so that the result would be a neat and readable spreadsheet with dynamic amounts of data.
User avatar
Naomi Lastname
 
Posts: 3390
Joined: Mon Sep 25, 2006 9:21 am

Post » Mon Aug 02, 2010 7:56 pm

Other than the W3Schools links does anyone have any good links for defining my own schema? I want to allow XML files to be used with a program I wrote and I will allow people to write it themselves but I want to be sure if conforms to how my program will handle it such as element names.

There are tools for defining XML schemas, but it's possible to write it from scratch and it's not too complex. The file extension used is .xsd. The tools could allow you to start with an example file and create an XSD based on that, or create example files from the XSD.



...


Inside the xs:schema node you can define the sequence of elements, type of each element, the number of times they can appear in the file etc. You can define namespaces (the prefix) for the elements so some elements in your xml files could be defined in different schemas, but I suspect you don't need this.

Some usable data types: string, integer, boolean, decimal, time, date. Times and date have specific formats. For example date 2002-10-09T11:00:00Z, which is in UTC, but you can also express the time zone like this: 2002-10-09T13:00:00+0200. See ISO8601. Data types int and integer are both valid.

This would define an elemnent of type string

and another element of type int


You can use attributes minOccurs="2" and maxOccurs"10" to bound the number of times the element can appear, or even maxOccurs="unbounded".


A sequence defines the order in which the elements appear
    

Here you would define an element Customer with subnodes in sequence.
                         


That should be enough to get started. In some cases, you could consider using attributes, but use them very sparingly. The nodes should have the data, but attributes can be used to give additional information for the data. The xml looks a lot neater to read like that..
  
22 Street, Honolulu
1

I copied some information from here and there's more on attributes too.
http://www.liquid-technologies.com/Tutorials/XmlSchemas/XsdTutorial_01.aspx
User avatar
QuinDINGDONGcey
 
Posts: 3369
Joined: Mon Jul 23, 2007 4:11 pm

Post » Tue Aug 03, 2010 9:04 am

There are tools for defining XML schemas, but it's possible to write it from scratch and it's not too complex. The file extension used is .xsd.

Note: There is also DTD and RELAX NG, which are both in common usage as XML Schema languages. There are also a load more, but those (along with XML Schema) are the common ones.

DTD is simple, but fairly limited compared to the others.
User avatar
Christina Trayler
 
Posts: 3434
Joined: Tue Nov 07, 2006 3:27 am

Post » Mon Aug 02, 2010 7:46 pm

So I am assuming I would need to supply the schema somehow to the users for them to verify the XML? How would they do it or would I just compare the structure to trhe schema in my application?
User avatar
Javaun Thompson
 
Posts: 3397
Joined: Fri Sep 21, 2007 10:28 am

Post » Tue Aug 03, 2010 7:33 am

DTD is limited compared to the only XML validation I've actually used at work. I learned DTD in the university, only to realize that in real life there is something else and they are already using the new.

XML validation is typically done on the receiving end if we are talking about message passing to make sure every party sticks to the standard. You don't really need the schema during sending as long as the created file by the sender passes the validation. Typically XML's are also created from data by a program, so why would it not pass the validation if the program was implemented according to the specification in the first place so in that kind of context validation by the sender is kinda redundant after development phase.

That's how XML messages are typically passed from sender to receiver, but you can of course define a different process. Maybe you have plenty of user input as a part of the xml creation and user input should always be validated one way or the other, and I'm not necessarily referring to schemas. User could feed illegal data into the fields that could break well-formedness of the xml file, not just schema validation.
User avatar
Cool Man Sam
 
Posts: 3392
Joined: Thu May 10, 2007 1:19 pm

Post » Tue Aug 03, 2010 4:19 am

DTD is limited compared to the only XML validation I've actually used at work. I learned DTD in the university, only to realize that in real life there is something else and they are already using the new.

XML validation is typically done on the receiving end if we are talking about message passing to make sure every party sticks to the standard. You don't really need the schema during sending as long as the created file by the sender passes the validation. Typically XML's are also created from data by a program, so why would it not pass the validation if the program was implemented according to the specification in the first place so in that kind of context validation by the sender is kinda redundant after development phase.

That's how XML messages are typically passed from sender to receiver, but you can of course define a different process. Maybe you have plenty of user input as a part of the xml creation and user input should always be validated one way or the other, and I'm not necessarily referring to schemas. User could feed illegal data into the fields that could break well-formedness of the xml file, not just schema validation.

Well I am going to code a part of my program to write XML but if the user chooses to write it themself I need someway to make sure its int he right format before my program tries to read it. I think I cn do it with a simple if/else check for each node and end the operation if one is wrong. Might be a bit cumbersome to do though for each node.
User avatar
Alkira rose Nankivell
 
Posts: 3417
Joined: Tue Feb 27, 2007 10:56 pm

Post » Tue Aug 03, 2010 4:21 am

Well, you can do it the way you want in that case. Schema validation would give you a somewhat meaningful error message even if you did it at the sending end. I'm only describing one way xml messages are used in communication and the xml in my case is created by the system, not the user by hand. I left out the part after schema validation where the receiver sends an acknowledgement back to the sender telling the original message was ok, or rejected, and why. If you change the format at some point, you would most likely need to update both the schema and the application creating the message anyways.. The application still needs to validate user xml input somehow and schema would be the least amount of work unless your format was really simple.

You know that IE can handle XML files and it even does DTD validation. Stylesheets too. However, I hear that IE has been "updated" so that the DTD file must now exist in the same domain, or it gives you an error. Technically, it should be possible to use a web site to host the common schema/DTD and refer to that in the xml, but you may not want to do that if it keeps loading the file on each validation of any message. If the format was simple enough, DTD might be enough.
User avatar
Robert Garcia
 
Posts: 3323
Joined: Thu Oct 11, 2007 5:26 pm

Post » Tue Aug 03, 2010 12:47 am

Does anyone else use the gtk+ libraries for gui designing? I hate them. Overly complicated.

I prefer http://en.wikipedia.org/wiki/Qt_%28framework%29.
User avatar
joannARRGH
 
Posts: 3431
Joined: Mon Mar 05, 2007 6:09 am

Post » Mon Aug 02, 2010 7:15 pm

I prefer http://en.wikipedia.org/wiki/Qt_%28framework%29.

Never heard of that. I've been having trouble getting checkboxes in a treeview to work. Followed every online example, doesn't seem to want to work.
User avatar
FoReVeR_Me_N
 
Posts: 3556
Joined: Wed Sep 05, 2007 8:25 pm

Post » Mon Aug 02, 2010 9:38 pm

I give up on this. I need some checkboxes in a treeview that I can check on or off. Heres the code. The gui libraries are gtk#.
Spoiler
		private void InitTreeView ()		{			AllAssignments Assignments = new AllAssignments ();			DirectoryData DirData = http://forums.bethsoft.com/index.php?/topic/1166756-unofficial-programming-thread-iii/new DirectoryData ();			// Put data into the TreeStore.			TreeViewData = new Gtk.TreeStore (typeof(bool), typeof(string), typeof(string), typeof(string));			for (int a = 0; a < Directory.GetFiles (DirData.AssignmentDir).Length; a++) {				TreeViewData.AppendValues (true, Assignments.Names[a], Assignments.Subjects[a], Assignments.DueDates[a].ToShortDateString ());			}			// Make all the cell renders.			//Gtk.CellRendererToggle CheckRender = new Gtk.CellRendererToggle ();			//Gtk.CellRendererText NameRender = new Gtk.CellRendererText ();			//Gtk.CellRendererText SubjectRender = new Gtk.CellRendererText ();			//Gtk.CellRendererText DateRender = new Gtk.CellRendererText ();			// Make the treeview columns.			Gtk.TreeViewColumn CheckColumn = new Gtk.TreeViewColumn ("Done", new Gtk.CellRendererToggle (), "activatable", 0);			Gtk.TreeViewColumn NameColumn = new Gtk.TreeViewColumn ("Name", new Gtk.CellRendererText (), "text", 1);			Gtk.TreeViewColumn SubjectColumn = new Gtk.TreeViewColumn ("Subject", new Gtk.CellRendererText (), "text", 2);			Gtk.TreeViewColumn DateColumn = new Gtk.TreeViewColumn ("Due Date", new Gtk.CellRendererText (), "text", 3);			// Set the columns to be resizeable.			NameColumn.Resizable = true;			SubjectColumn.Resizable = true;			DateColumn.Resizable = true;			// Adds the columns to the treeview.			this.treeview1.AppendColumn (CheckColumn);			this.treeview1.AppendColumn (NameColumn);			this.treeview1.AppendColumn (SubjectColumn);			this.treeview1.AppendColumn (DateColumn);			this.treeview1.Model = TreeViewData;		}

User avatar
Andrew
 
Posts: 3521
Joined: Tue May 08, 2007 1:44 am

Post » Tue Aug 03, 2010 9:29 am

Never heard of that. I've been having trouble getting checkboxes in a treeview to work. Followed every online example, doesn't seem to want to work.

Qt was originally a product of Trolltech. It has open and a commercial versions for many platform. Good piece of technology and the programming interface is fairly easy to work with. Then Nokia bought the whole company, and the new CEO lead Nokia away and towards the Windows platform, effectively running down the other technologies as far as I know. So, no matter, if you have not heard of Qt. We'll have to see how long it lasts, but hopefully the development is continued after all. I did my thesis work with Qt3, but never tried gtk.

What is not working in your code? You have the column visible, but you can't check it on or off. Is that it? You may need a handler that changes the status according to this.
http://www.go-mono.com/docs/index.aspx?link=T:Gtk.CellRendererToggle
User avatar
jenny goodwin
 
Posts: 3461
Joined: Wed Sep 13, 2006 4:57 am

Post » Tue Aug 03, 2010 7:22 am

Anyone know of a program I can use to print my code without headers? For soem reason Visual Studio 2010 won't print the syntax highlighting, I found an addin for it to do it but it prints the path and file name as a header which I do not want. Notepad++ prints the syntax highlighting but also headers. Line numbers are important as well.

If I copy my code to word then it adds line numbers to lines that wrap around which throws things off.

Any other suggestions for me?
User avatar
Marie Maillos
 
Posts: 3403
Joined: Wed Mar 21, 2007 4:39 pm

Post » Tue Aug 03, 2010 9:31 am

Why would you want to print code? I can't imagine anything more useless than 30+ pages of code that are out of date as soon as you alter you main codebase. :shrug:
User avatar
yermom
 
Posts: 3323
Joined: Mon Oct 15, 2007 12:56 pm

Post » Tue Aug 03, 2010 9:10 am

Hey, some people like doing that. We have a guy at work who does that and after a year he has a 3 foot pile of prints on his desk, all code apparently. Well, if it helps him to interpret the existing code, let him. :)

Sorry, I haven't tried to print code. Class diagrams and flow charts in the design phase I may have.
User avatar
Amy Melissa
 
Posts: 3390
Joined: Fri Jun 23, 2006 2:35 pm

Post » Tue Aug 03, 2010 7:52 am

What is not working in your code? You have the column visible, but you can't check it on or off. Is that it? You may need a handler that changes the status according to this.
http://www.go-mono.com/docs/index.aspx?link=T:Gtk.CellRendererToggle

I tried that exact example so many times. Never worked. :brokencomputer:
User avatar
Mrs. Patton
 
Posts: 3418
Joined: Fri Jan 26, 2007 8:00 am

Post » Mon Aug 02, 2010 10:51 pm

Anyone know of a program I can use to print my code without headers? For soem reason Visual Studio 2010 won't print the syntax highlighting, I found an addin for it to do it but it prints the path and file name as a header which I do not want. Notepad++ prints the syntax highlighting but also headers. Line numbers are important as well.

If I copy my code to word then it adds line numbers to lines that wrap around which throws things off.

Any other suggestions for me?

You can turn off the printing of a header in Notepad++. Settings->Preferences->Print.

I can't imagine printing code as ever being useful myself, but to each their own I guess.
User avatar
CArlos BArrera
 
Posts: 3470
Joined: Wed Nov 21, 2007 3:26 am

Next

Return to Othor Games