Zipping files within a XML

  • (2 Pages)
  • +
  • 1
  • 2

22 Replies - 1357 Views - Last Post: 30 November 2012 - 04:38 PM Rate Topic: -----

#1 maylortaylor  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 15
  • Joined: 26-November 12

Zipping files within a XML

Posted 28 November 2012 - 09:27 AM

I have a console program that reads information from an XML file and then performs a zip on certain files.

-<Jobs> 
-<Job> 
<JobName>a</JobName> 
<Source>C:\Users\Public\Pictures\Sample Pictures\Lighthouse.jpg</Source> 
<Source>C:\Users\Public\Pictures\Sample Pictures\Penguins.jpg</Source> 
<Destination>C:\Users\Public\Pictures\Sample Pictures\a.zip</Destination> 
<Timestamp>11/26/2012 6:18:00 PM</Timestamp> 
</Job> 

-<Job> 
<JobName>b</JobName>
<Source>C:\Users\Public\Pictures\demo photo\1 - Copy.JPG</Source> 
<Source>C:\Users\Public\Pictures\demo photo\1.JPG</Source> 
<Destination>C:\Users\Public\Pictures\demo photo\b.zip</Destination> 
<Timestamp>11/26/2012 6:18:19 PM</Timestamp> 
</Job> 
</Jobs>


This is the XMl file i'm reading. Right now, the code (found below) wil read each node and output it to the console. It will also zip ALL the files found within the 'source' nodes and create two zips (in both 'destination' node). However, i want each 'job' to be seperate. I want "Job A" zip the files located in that job (Lighthouse.jpg & penguins.jpg) and store the zip in the respective destination. But my problem comes at this point. The program zips all the 'source' nodes together and makes two seperate zips with all the files contained within. Check my code..i'm sure it's something simple.

Sub main()
        

        Dim doc As New System.Xml.XmlDocument
        doc.Load("C:\users\matt taylor\desktop\backup\backup.xml")
        
        Dim JobNodes As XmlNodeList
        Dim JobNode As XmlNode
        Dim baseDataNodes As XmlNodeList
       

        JobNodes = doc.GetElementsByTagName("Job")
        For Each jobNode In JobNodes
            baseDataNodes = JobNode.ChildNodes()
            
            

            For Each baseDataNode As XmlElement In baseDataNodes

                Dim Source = baseDataNode.SelectNodes("descendant::Source")
                Dim Destin = baseDataNode.SelectNodes("descendant::Destination")

                Using zip As New ZipFile()

                    For Each item As System.Xml.XmlNode In Source
                        zip.AddFile(item.InnerText, "Archive_" & DateString)

                    Next

                    For Each item As System.Xml.XmlNode In Destin
                        zip.Save(item.InnerText)
                    Next
                End Using

                Console.Write(vbCrLf)

                Console.Write(baseDataNode.Name & ": " & baseDataNode.InnerText)
            Next
            
            Console.Write(vbCrLf)
            Console.Write(vbCrLf)
        Next
        
        Console.Read()



    End Sub


Is This A Good Question/Topic? 0
  • +

Replies To: Zipping files within a XML

#2 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5537
  • View blog
  • Posts: 11,868
  • Joined: 02-June 10

Re: Zipping files within a XML

Posted 28 November 2012 - 09:49 AM

Personally I'd start by cleaning up your archetecture.
Instead of one big method break it down into descreete functions with one purpose in life.

1 - Read the XML into a collection of JOB objects. Your XML file should become a List<JOB> collection of objects.

-<Job> 
<JobName>a</JobName> 
<Source>C:\Users\Public\Pictures\Sample Pictures\Lighthouse.jpg</Source> 
<Source>C:\Users\Public\Pictures\Sample Pictures\Penguins.jpg</Source> 
<Destination>C:\Users\Public\Pictures\Sample Pictures\a.zip</Destination> 
<Timestamp>11/26/2012 6:18:00 PM</Timestamp> 
</Job>

is seperate from
-<Job> 
<JobName>b</JobName>
<Source>C:\Users\Public\Pictures\demo photo\1 - Copy.JPG</Source> 
<Source>C:\Users\Public\Pictures\demo photo\1.JPG</Source> 
<Destination>C:\Users\Public\Pictures\demo photo\b.zip</Destination> 
<Timestamp>11/26/2012 6:18:19 PM</Timestamp> 
</Job> 
</Jobs>


Give the JOB class a method for doing each thing:
  • DisplayToConsole()
  • ZipToArchive()


When you start designing in an OOP fashion this stuff really just becomes a lot easier. Classes & Objects tutorial linked in my signature block
Was This Post Helpful? 3
  • +
  • -

#3 maylortaylor  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 15
  • Joined: 26-November 12

Re: Zipping files within a XML

Posted 29 November 2012 - 08:57 AM

Ok, i understand that my code is ugly and messy - but it's because i am pretty new to coding things on this scale.

Is there any advice you can give me that utilizes MY code? I just don't feel like reworking the entire thing right now.
Was This Post Helpful? 0
  • +
  • -

#4 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5537
  • View blog
  • Posts: 11,868
  • Joined: 02-June 10

Re: Zipping files within a XML

Posted 29 November 2012 - 09:16 AM

I don't tell people how to band-aid their way out of bad code.

All it does it promote the use of bad code. The next person that reads this thread then says "But tlhIn`toq said to do it this way, and he's a Mentor." Then invariably a week you or someone that mimics your code posts a follow up question:

Quote

"I continued along the way you said not to go because I already had an hour invested in it. Now I am painted into a corner and I have 10 more hours invested. Please help me fix another issue with this caused by the design."


Quote

I just don't feel like reworking the entire thing right now.

Its your code. You don't have to do anything you don't want to do. If you want to pursue a non-OOP design then do that. But I'll say that the longer you drag out shifting your style to good OOP design the more firmly you dig in those bad habits... the harder it will be to unlearn them in the future.
Was This Post Helpful? 3
  • +
  • -

#5 maylortaylor  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 15
  • Joined: 26-November 12

Re: Zipping files within a XML

Posted 29 November 2012 - 12:22 PM

ok. i concede. I must do your way (the best way).

How do i go about doing the first thing you suggested "1 - Read the XML into a collection of JOB objects. Your XML file should become a List<JOB> collection of objects."

Keep in mind, i'm new to vb - so be gentle
Was This Post Helpful? 0
  • +
  • -

#6 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5537
  • View blog
  • Posts: 11,868
  • Joined: 02-June 10

Re: Zipping files within a XML

Posted 29 November 2012 - 12:38 PM

Ok - lets start with classes and objects. Do you understand exactly what a class is and what an object is? (Tutorial linked in my signature block if not)

Before handling the reading of the XML consider this: When you do read it the goal is to make object instances of JOB. Therefore you need to have a JOB class defined so you can make instances of it.

So how about your write a JOB class with properties corresponding to your XML tags: Source, Name, Destination, TimeStamp
and stub out methods I mentioned in post #2.

Post your code for that class here when done and we will continue on.
Was This Post Helpful? 2
  • +
  • -

#7 maylortaylor  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 15
  • Joined: 26-November 12

Re: Zipping files within a XML

Posted 29 November 2012 - 01:29 PM

Something as simple as this?

Imports System.Xml
Imports Ionic.Zip

Public Class Job
    Dim Source As String
    Dim Name As String
    Dim Destination As String
    Dim TimeStamp As String




    Public Function DisplayToConsole()
       
    End Function

    Public Function ZipToArchive()
        

    End Function

End Class

Was This Post Helpful? 0
  • +
  • -

#8 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5537
  • View blog
  • Posts: 11,868
  • Joined: 02-June 10

Re: Zipping files within a XML

Posted 29 November 2012 - 02:53 PM

VB.NET is not my native coding language, C# is. So I don't 'think' in VB but do half-assed okay at reading it.

Source, name, Destination and Timestamp here however are not properties. They are fields. Properties have built-in Get and Set methods. For example:
Public Property name() As String
   Get
      Return _name.ToString
   End Get
   Set(ByVal value As String)
       _name = value
   End Set
End Property

As a rule properties are public as a means of interaction outside of their class and fields are private for internal use. That's just a loose rule. Often properties can be private for internal use because its a lot easier to have the Get and Set inside the property than to use getter and setter methods (which some languages have to use)

The tutorial linked below on properties is written for C# but the important underlying concepts should make sense and I would hope you can make enough sense of the code snippets to see the logic in using them.

How about reading that article then updating your JOB class to use public properties instead of those fields.
Was This Post Helpful? 0
  • +
  • -

#9 maylortaylor  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 15
  • Joined: 26-November 12

Re: Zipping files within a XML

Posted 29 November 2012 - 03:33 PM

Ok, i have the blank shell of all the properties. But i'm cluless on how to grab the node (tags) from the XML file and assign it to the property..
Public Class Job
    Public name As String
    Public source As String
    Public destination As String
    Public timeStamp As String


    Public Property jName() As String
        Get
            Return name
        End Get
        Set(value As String)
            name = value
        End Set
    End Property
    Public Property jsource() As String
        Get
            Return source

        End Get
        Set(value As String)
            source = value
        End Set
    End Property
    Public Property jdestination() As String
        Get
            Return destination
        End Get
        Set(value As String)
            destination = value
        End Set
    End Property
    Public Property jTimeStamp() As String
        Get
            Return timeStamp
        End Get
        Set(value As String)
            timeStamp = value
        End Set
    End Property

Was This Post Helpful? 0
  • +
  • -

#10 AdamSpeight2008  Icon User is offline

  • MrCupOfT
  • member icon


Reputation: 2264
  • View blog
  • Posts: 9,470
  • Joined: 29-May 08

Re: Zipping files within a XML

Posted 29 November 2012 - 04:53 PM

Here is a example of a Class, Field, Property, Method and Constructor
Public Class _ExampleClass_
  Dim _BackingFieldFor_ThisProperty_ As String ' Dim => Private 

  Public Property _ThisProperty_() As String
     Get
       Return _BackingFieldFor_ThisProperty_ 
     End Get
     Set(value As String)
        _BackingFieldFor_ThisProperty_ = value
     End Set
  
  ' The Constructor
  Public Sub New(Value As String )
    Me._ThisProperty = Value
  End Sub

  Public Sub WriteToConsole()
    Console.WriteLine(  Me._ThisProperty_ )
  End Sub
   
End Class






VB.net has pretty good support for XML.
Something similar to this could do it.

For Each j In xml...<Job>
  Dim Dest   = j...<Destination>.FirstOrDefault().Value
  If Dest IsNot Nothing Then
    For Each source In j...<Source>
      ZipFile.Add(source.Value)
    Next 
  End If
Next


This post has been edited by AdamSpeight2008: 29 November 2012 - 04:55 PM

Was This Post Helpful? 4
  • +
  • -

#11 maylortaylor  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 15
  • Joined: 26-November 12

Re: Zipping files within a XML

Posted 30 November 2012 - 08:44 AM

View PostAdamSpeight2008, on 29 November 2012 - 04:53 PM, said:

Here is a example of a Class, Field, Property, Method and Constructor
Public Class _ExampleClass_
  Dim _BackingFieldFor_ThisProperty_ As String ' Dim => Private 

  Public Property _ThisProperty_() As String
     Get
       Return _BackingFieldFor_ThisProperty_ 
     End Get
     Set(value As String)
        _BackingFieldFor_ThisProperty_ = value
     End Set
  
  ' The Constructor
  Public Sub New(Value As String )
    Me._ThisProperty = Value
  End Sub

  Public Sub WriteToConsole()
    Console.WriteLine(  Me._ThisProperty_ )
  End Sub
   
End Class






VB.net has pretty good support for XML.
Something similar to this could do it.

For Each j In xml...<Job>
  Dim Dest   = j...<Destination>.FirstOrDefault().Value
  If Dest IsNot Nothing Then
    For Each source In j...<Source>
      ZipFile.Add(source.Value)
    Next 
  End If
Next




I hate to be a noob, but what should go in the elipses(...) that you have in your last code.

Also, the code i have below leaves me some odd output. It posts "System.xml.XPathnodeList" for each item. I looked in the Locals screen and under 'myjob' (which is how i'm calling the class i just made) each item : jobName, jobSource, jobDestination, etc...all have this value.

Dim doc As New System.Xml.XmlDocument
        Dim myJob As New Atr.backup.Job

        doc.Load("C:\users\matt taylor\desktop\backup\backup.xml")
       
        Dim JobNodes As XmlNodeList
        Dim JobNode As XmlNode
        Dim baseDataNodes As XmlNodeList


        JobNodes = doc.GetElementsByTagName("Job")

        For Each JobNode In JobNodes
            baseDataNodes = JobNode.ChildNodes()


            For Each baseDataNode As XmlNode In baseDataNodes

                myJob.jobName = baseDataNode.SelectNodes("JobName").ToString
                myJob.jobSource = doc.GetElementsByTagName("Source").ToString
                myJob.jobDestination = doc.GetElementsByTagName("Destination").ToString
                myJob.jobTimeStamp = doc.GetElementsByTagName("Timestamp").ToString

                Dim Source = baseDataNode.SelectNodes("Source")
                Dim Destin = baseDataNode.SelectNodes("Destination")

                Using zip As New ZipFile()
                    For Each j As System.Xml.XmlNode In Source
                        zip.AddFile(j.InnerText, "Archive_" & DateString)

                    Next

                    For Each item As System.Xml.XmlNode In Destin
                        zip.Save(item.InnerText)
                    Next
                End Using

                Console.Write(vbCrLf)

                Console.WriteLine(myJob.jobName)
                
            Next

            Console.Write(vbCrLf)
            Console.Write(vbCrLf)
        Next

        Console.Read()
    End Sub

Was This Post Helpful? 0
  • +
  • -

#12 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5537
  • View blog
  • Posts: 11,868
  • Joined: 02-June 10

Re: Zipping files within a XML

Posted 30 November 2012 - 08:49 AM

Hey Adam, just wanted to say 'thanks' for jumping in on this. I'm glad the OP didn't go without help just because I had to spend an evening in the real world. Your VB knowledge is far superior to mine and won't lead him off on wrong paths like mine would at this point.
Was This Post Helpful? 1
  • +
  • -

#13 AdamSpeight2008  Icon User is offline

  • MrCupOfT
  • member icon


Reputation: 2264
  • View blog
  • Posts: 9,470
  • Joined: 29-May 08

Re: Zipping files within a XML

Posted 30 November 2012 - 09:09 AM

Nothing should go between the ... its part of the XML Literal syntax.

... roughly means find matching node(s) at any depth.

Tutorial on XML Literals.




.ToString() is often left to the default of the base class, which is the fully qualified namespace.
Eg doing a .ToString on an array doesn't a string containing the elements in the array.
Best thing to do is consult the documentation for that class on MSDN.

This post has been edited by AdamSpeight2008: 30 November 2012 - 09:14 AM

Was This Post Helpful? 0
  • +
  • -

#14 maylortaylor  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 15
  • Joined: 26-November 12

Re: Zipping files within a XML

Posted 30 November 2012 - 09:35 AM

I got 'cha now Adam. Had to look up some XMl literal info online. Seems to be the best thing for me at this point.

However, in that zip code you had at the bottom of your post; the 'For each j in xml...<job>' line...i get the "system.xml is a namespace and cannot be used as an expression.

Also, any clue to where the problem may be with the "System.xml.XPathnodeList" showing up on the console.
Was This Post Helpful? 0
  • +
  • -

#15 AdamSpeight2008  Icon User is offline

  • MrCupOfT
  • member icon


Reputation: 2264
  • View blog
  • Posts: 9,470
  • Joined: 29-May 08

Re: Zipping files within a XML

Posted 30 November 2012 - 09:44 AM

What do want cut & paste? Develop your analytic and debugging skills! What do you think that xml represents or means? Given that information how can it be applied to your work.

This post has been edited by AdamSpeight2008: 30 November 2012 - 12:03 PM

Was This Post Helpful? 1
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2