+ Reply to Thread
Results 1 to 29 of 29

Creating an XML file with a specific schema

Hybrid View

  1. #1
    Registered User
    Join Date
    04-20-2015
    Location
    Madrid
    MS-Off Ver
    2010
    Posts
    85

    Creating an XML file with a specific schema

    I would need some help with creating an XML file which has the schema (or structure) as in the attached image.

    I would like to generate the XML file from the Excel file that I have attached.

    It would be greatly appreciated if someone can instruct me with step-by-step explanation as to how to create the XML with the given schema.

    Much obliged!
    Attached Images Attached Images
    Attached Files Attached Files

  2. #2
    Valued Forum Contributor Naveed Raza's Avatar
    Join Date
    11-04-2012
    Location
    India, Hyderabad
    MS-Off Ver
    Excel, Access 2007/2010
    Posts
    1,338

    Re: Creating an XML file with a specific schema

    Hi Papusale

    u have some blank value in Product_ID_Required column
    Thanks - Naveed
    -----------------------------
    If the suggestion helps you, then Click * to Add Reputation
    To Attach File: Go Advanced>>Manage Attachments>>Add Files (In Top Right Corner)>>SelectFiles>>.........Locate Your File(s)>>Upload Files>>Done (In Bottom Right)
    1. Use [code] code tags [\code]. It keeps posts clean, easy-to-read, and maintains VBA formatting.
    2. If you are happy with a solution to your problem, mark the thread as [SOLVED] using the tools at the top.

  3. #3
    Registered User
    Join Date
    04-20-2015
    Location
    Madrid
    MS-Off Ver
    2010
    Posts
    85

    Re: Creating an XML file with a specific schema

    Quote Originally Posted by Naveed Raza View Post
    Hi Papusale

    u have some blank value in Product_ID_Required column
    Hi Naveed,

    Yes this is true. However for any given title (column), content will not be mandatory, meaning that blanks are allowed.
    PS: Ignore the names given in the XML example. Even if it says "Product_ID_Required", it's really not required to have any content.

  4. #4
    Valued Forum Contributor Naveed Raza's Avatar
    Join Date
    11-04-2012
    Location
    India, Hyderabad
    MS-Off Ver
    Excel, Access 2007/2010
    Posts
    1,338

    Re: Creating an XML file with a specific schema

    so U mean to say for blank also u want me to show in XML ? is it right , if so I will be back in 30 minutes

  5. #5
    Registered User
    Join Date
    04-20-2015
    Location
    Madrid
    MS-Off Ver
    2010
    Posts
    85

    Re: Creating an XML file with a specific schema

    Quote Originally Posted by Naveed Raza View Post
    so U mean to say for blank also u want me to show in XML ? is it right , if so I will be back in 30 minutes
    Yes Naveed, that is correct .

    Thank you very much for your help Naveed. Much appreciated!

  6. #6
    Registered User
    Join Date
    04-20-2015
    Location
    Madrid
    MS-Off Ver
    2010
    Posts
    85

    Re: Creating an XML file with a specific schema

    Hi Naveed (or anyone else who may help),

    Don't mean to bother, but can I kindly ask for an update on this request?
    I would have to have my project done by Monday, so I'm quite under pressure.

    Really appreciate your help! Thank you again

  7. #7
    Valued Forum Contributor Naveed Raza's Avatar
    Join Date
    11-04-2012
    Location
    India, Hyderabad
    MS-Off Ver
    Excel, Access 2007/2010
    Posts
    1,338

    Re: Creating an XML file with a specific schema

    hi Papusale

    your data has some invalid name like (ä & ö & å) and I have removed invalid letters from some rows and tried & its worked perfectly(you can find data in sheet1 on which I run the macro)

    please find the code below & attached file

    
    Sub XMLFIle()
      Dim strXML As String
        'strXML = NavfGenerateXML(Selection, "IndividualAccounts")
        Dim HEADER As String
        Dim TAG_BEGIN  As String
        Dim TAG_END  As String
        Dim LC As Long
        Dim LR As Long
        Dim Btag As String
        Dim filenameinput As String
        Dim FPath As String, FName As String
    '========================================
    Dim Sht As Worksheet: Set Sht = ThisWorkbook.Sheets("Sheet1")
    FPath = "C:\Users\NaveedM\Downloads\Excel Forum" '<--- change the path suit to you
    FName = "XMLTest" '<--- change the name suit to you
    filenameinput = FPath & "\" & FName & ".xml"
    
    
    HEADER = "<?xml version=""1.0"" encoding=""UTF-8"" ?>" & vbCrLf
    strXML = HEADER
     
        
       TAG_BEGIN = "<Products>"
       TAG_END = "</Products>"
    
        strXML = strXML & TAG_BEGIN
        
        With Sht
            'Finding Last Row
            LR = .Range("A" & .Rows.Count).End(xlUp).Row
            LC = 7
                    
            For i = 2 To LR
                strXML = strXML & vbCrLf & "<Product>"
                
                For j = 1 To LC
                    If .Cells(i, j).Value = "" Then
                        strXML = strXML & vbCrLf & "<" & .Cells(2, j).Value & "/>"
                    Else
                        strXML = strXML & vbCrLf & "<" & .Cells(1, j).Value & ">" & .Cells(i, j).Value & "</" & .Cells(1, j).Value & ">"
                    End If
                Next
                j = 1
                strXML = strXML & vbCrLf & "</Product>"
            Next
       End With
       strXML = strXML & TAG_END
    '=========================================
        sWriteFile strXML, filenameinput
        MsgBox ("Completed. XML Written to " & filenameinput)
    End Sub
    ' Function for writing plain string out a file
    Sub sWriteFile(strXML As String, strFullFileName As String)
    
        Dim intFileNum As String
        
        intFileNum = FreeFile
        Open strFullFileName For Output As #intFileNum
        Print #intFileNum, strXML
        Close #intFileNum
    End Sub
    Attached Files Attached Files
    Last edited by Naveed Raza; 05-18-2015 at 02:59 AM.

  8. #8
    Registered User
    Join Date
    04-20-2015
    Location
    Madrid
    MS-Off Ver
    2010
    Posts
    85

    Re: Creating an XML file with a specific schema

    Thank you very much Naveed!

    Actually the (ä & ö & å) characters are not invalid, they are supposed to be there, as these are characters in Finnish language.
    Is it possible to create the code to also support these?

    Also, how do you actually create the XML file?

    I opened your modified excel file version and tried to save as XML file (data), and it says:
    "Cannot save XML data because the workbook does not contain any XML mappings."

    Again, thank you for everything
    Last edited by papusale; 05-18-2015 at 06:00 AM.

  9. #9
    Valued Forum Contributor Naveed Raza's Avatar
    Join Date
    11-04-2012
    Location
    India, Hyderabad
    MS-Off Ver
    Excel, Access 2007/2010
    Posts
    1,338

    Re: Creating an XML file with a specific schema

    Steps:

    1. Just the Open the Excel file
    2. Press Alt+F11 and double click on Module 1 and change the Path in script page suit to u
    3. Next, go back to excel file & press Alt + F8 and select XMLFile and click on Run

    it will create a XML file in that path which u have assign

  10. #10
    Registered User
    Join Date
    04-20-2015
    Location
    Madrid
    MS-Off Ver
    2010
    Posts
    85

    Re: Creating an XML file with a specific schema

    Quote Originally Posted by Naveed Raza View Post
    Steps:

    1. Just the Open the Excel file
    2. Press Alt+F11 and double click on Module 1 and change the Path in script page suit to u
    3. Next, go back to excel file & press Alt + F8 and select XMLFile and click on Run

    it will create a XML file in that path which u have assign
    It works! Simply amazing Naveed! I greatly appreciate your help!

    So I will simply copy and paste the Module1 code to any other new excel file I want to create an XML file with?
    Also, will it automatically generate other data missing from this example excel file (that are listed in the first XML schema example), such as "Product_Condition_Required", "Language_Optional" etc?
    Because some other excel files will have some data that is missing from the one I attached.

    PS: The original XML example file has "<unapproved xmlns:sql="urn:schemas-microsoft-com:xml-sql">" at the top, but missing from the module you created. Will this make any kind of difference in terms of functionality?

  11. #11
    Valued Forum Contributor Naveed Raza's Avatar
    Join Date
    11-04-2012
    Location
    India, Hyderabad
    MS-Off Ver
    Excel, Access 2007/2010
    Posts
    1,338

    Re: Creating an XML file with a specific schema

    Hi Papusale

    just make that attached file as your Macro Tool..

    1. Just copy your original Data and dump in that attached file (Sheet1 Tab) as per header.
    2. don't rename the 1st tab.
    3. Next run the macro that's it

    Note: that Invalid Letters should not be there if there then xml file will not create properly

    if you got your solution please mark the spread as SOLVED and click on * Add Reputation to say thanks don't forget

  12. #12
    Valued Forum Contributor Naveed Raza's Avatar
    Join Date
    11-04-2012
    Location
    India, Hyderabad
    MS-Off Ver
    Excel, Access 2007/2010
    Posts
    1,338

    Re: Creating an XML file with a specific schema

    Hi Papusale

    just make that attached file as your Macro Tool..

    1. Just copy your original Data and dump in that attached file (Sheet1 Tab) as per header.
    2. don't rename the 1st tab.
    3. Next run the macro that's it

    Note: that Invalid Letters should not be there if there then xml file will not create properly

    if you got your solution please mark the spread as SOLVED and click on * Add Reputation to say thanks don't forget

  13. #13
    Registered User
    Join Date
    04-20-2015
    Location
    Madrid
    MS-Off Ver
    2010
    Posts
    85

    Re: Creating an XML file with a specific schema

    Quote Originally Posted by Naveed Raza View Post
    Hi Papusale

    just make that attached file as your Macro Tool..

    1. Just copy your original Data and dump in that attached file (Sheet1 Tab) as per header.
    2. don't rename the 1st tab.
    3. Next run the macro that's it

    Note: that Invalid Letters should not be there if there then xml file will not create properly

    if you got your solution please mark the spread as SOLVED and click on * Add Reputation to say thanks don't forget
    Hi Naveed,

    I added a reputation for you.
    However there's one thing missing until I change the status to SOLVED.
    It's what I asked in my previous post:

    "Also, will it automatically generate other data missing from this example excel file (that are listed in the first XML schema example), such as "Product_Condition_Required", "Language_Optional" etc?
    Because some other excel files will have some data that is missing from the one I attached."

  14. #14
    Valued Forum Contributor Naveed Raza's Avatar
    Join Date
    11-04-2012
    Location
    India, Hyderabad
    MS-Off Ver
    Excel, Access 2007/2010
    Posts
    1,338

    Re: Creating an XML file with a specific schema

    Thanks for the Reputation

    yeah it will automatically generate XML file just u need to dump your data in that attached file in Sheet1 Tab and run the macro that's it, it will generate automatically ....

  15. #15
    Registered User
    Join Date
    04-20-2015
    Location
    Madrid
    MS-Off Ver
    2010
    Posts
    85

    Re: Creating an XML file with a specific schema

    Quote Originally Posted by Naveed Raza View Post
    Thanks for the Reputation

    yeah it will automatically generate XML file just u need to dump your data in that attached file in Sheet1 Tab and run the macro that's it, it will generate automatically ....
    Hi Naveed,

    I have tried exactly what you have instructed with a new list, but unfortunately it will not include any new data types (I added "Product_Condition_Required" and "Language_Optional"). However it does generate the content for the rest of the data types (Title_Required etc.).

  16. #16
    Valued Forum Contributor Naveed Raza's Avatar
    Join Date
    11-04-2012
    Location
    India, Hyderabad
    MS-Off Ver
    Excel, Access 2007/2010
    Posts
    1,338

    Re: Creating an XML file with a specific schema

    You mean to say you have added new two more column right , if its so then replace below code

    LC = 7
    with this

    LC = .Cells(1, .Columns.Count).End(xlToLeft).Column

  17. #17
    Registered User
    Join Date
    04-20-2015
    Location
    Madrid
    MS-Off Ver
    2010
    Posts
    85

    Re: Creating an XML file with a specific schema

    Quote Originally Posted by Naveed Raza View Post
    You mean to say you have added new two more column right , if its so then replace below code

    LC = 7
    with this

    LC = .Cells(1, .Columns.Count).End(xlToLeft).Column
    Simply amazing!
    Everything is solved!

    There's just one thing Naveed, and I apologize for this, but it's important; the special characters are actually required, so leaving out ä, ö, å etc. is out of the question for my project.
    Do you have any suggestions?
    After this, the whole thing will be SOLVED!

  18. #18
    Registered User
    Join Date
    04-20-2015
    Location
    Madrid
    MS-Off Ver
    2010
    Posts
    85

    Re: Creating an XML file with a specific schema

    Hi Naveed,

    So it seems that the module does not generate the XML correctly when I insert the full element list for the XML.

    Attached is the excel file for which I want to generate the XML.

    It seems that other special characters (such as "&") are not allowed. However these characters are necessary, since they are included in some URLs etc.
    Attached Files Attached Files
    Last edited by papusale; 05-28-2015 at 12:03 PM.

  19. #19
    Valued Forum Contributor Naveed Raza's Avatar
    Join Date
    11-04-2012
    Location
    India, Hyderabad
    MS-Off Ver
    Excel, Access 2007/2010
    Posts
    1,338

    Re: Creating an XML file with a specific schema

    Hi Papusale

    please find the updated version

    please assign Fpath otherthan C drive and try , and one more thing those cells is blank for that u will get only close bracket in XML file

    please have test and let me know

  20. #20
    Registered User
    Join Date
    04-20-2015
    Location
    Madrid
    MS-Off Ver
    2010
    Posts
    85

    Re: Creating an XML file with a specific schema

    Quote Originally Posted by Naveed Raza View Post
    Hi Papusale

    please find the updated version

    please assign Fpath otherthan C drive and try , and one more thing those cells is blank for that u will get only close bracket in XML file

    please have test and let me know
    Hello Naveed,

    Yes this newer version works with blank content as well, but this is not a problem, because I can always replace blank cells with "-" or "None".

    The problem is the special characters. As I mentioned, since some websites have the letter "&" in their URL, and your module currently does not support that letter, it will not be possible to create an XML based on those websites.

    I will require all special characters (such as ä,ö,å etc.). It is very important, otherwise the whole module will be obsolete.

    In the XML schema example file, you can see that there are actually special characters inside the brackets (both & and ä), so it should be possible.

  21. #21
    Registered User
    Join Date
    05-29-2015
    Location
    Germany
    MS-Off Ver
    2010
    Posts
    27

    Re: Creating an XML file with a specific schema

    tl;dr:

    1) have you tried xml add-in? Your xml schema has a very simple structure, so I would suggest use this add-in.

    Just follow steps at http://bitwizards.com/blog/november-...rksheet-to-xml. It's much more convenient than macro, because any change in schema can be easily reflected in the map (xsd file) and you don't have to go through the VBA code.


    2) This can be also solved by formulas:
    ="<Product>"&if(A1="","<Product_ID_Required/>","<Product_ID_Required>"&A1&"</Product_ID_Required>")&if(B1="","<Title_Required/>","<Title_Required>"&B1&"</Title_Required>")&...
    fill for the first row and then only expand down.
    Last edited by regresss; 05-30-2015 at 08:16 AM.

  22. #22
    Registered User
    Join Date
    04-20-2015
    Location
    Madrid
    MS-Off Ver
    2010
    Posts
    85

    Re: Creating an XML file with a specific schema

    Quote Originally Posted by regresss View Post
    tl;dr: have you tried xml add-in? Your xml schema has a very simple structure, so I would suggest use this add-in.
    I have tried the add in before, but since I'm not very savvy with Excel, I didn't know how to create one with my XML schema. Could you help perhaps?

  23. #23
    Registered User
    Join Date
    05-29-2015
    Location
    Germany
    MS-Off Ver
    2010
    Posts
    27

    Re: Creating an XML file with a specific schema

    Quote Originally Posted by papusale View Post
    I have tried the add in before, but since I'm not very savvy with Excel, I didn't know how to create one with my XML schema. Could you help perhaps?
    Sure. Fill

    <?xml version="1.0" encoding="UTF-8"?>
    <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
    <xs:element name="NewsItemsTable">
    <xs:complexType>
    <xs:sequence>
    <xs:element ref="NewsItemRow" minOccurs="0" maxOccurs="unbounded"/>
    </xs:sequence>
    </xs:complexType>
    </xs:element>
    <xs:element name="NewsItemRow">
    <xs:complexType>
    <xs:sequence>
    <xs:element name="Product_ID_Required" type="xs:string"/>
    <xs:element name="Title_Required" type="xs:string"/>
    <xs:element name="..." type="xs:string"/>
    <xs:element name="..." type="xs:string"/>
    <xs:element name="..." type="xs:string"/>
    <xs:element name="..." type="xs:string"/>
    <xs:element name="..." type="xs:string"/>

    </xs:sequence>
    </xs:complexType>
    </xs:element>
    </xs:schema>
    with your elements (column names). Then you save the file as map.xsd. It is important that the order of elements is same as order of columns in your worksheet. Once you have the add-in installed in Excel, click on Source, choose the xsd file, right click on the map in Excel, choose "Map Element", highlight your table with the header, click ok and export as xml file. Then you have you file.



    Basically, create a worksheet with data as at http://bitwizards.com/blog/november-...rksheet-to-xml and follow all the steps. Reproduce what they have done. Once you manage to do that, you can start modifying the xsd and apply it on your data.
    Last edited by regresss; 05-30-2015 at 08:30 AM.

  24. #24
    Registered User
    Join Date
    04-20-2015
    Location
    Madrid
    MS-Off Ver
    2010
    Posts
    85

    Re: Creating an XML file with a specific schema

    Quote Originally Posted by regresss View Post
    Sure. Fill



    with your elements (column names). Then you save the file as map.xsd. It is important that the order of elements is same as order of columns in your worksheet. Once you have the add-in installed in Excel, click on Source, choose the xsd file, right click on the map in Excel, choose "Map Element", highlight your table with the header, click ok and export as xml file. Then you have you file.



    Basically, create a worksheet with data as at http://bitwizards.com/blog/november-...rksheet-to-xml and follow all the steps. Reproduce what they have done. Once you manage to do that, you can start modifying the xsd and apply it on your data.
    Thanks regress! This actually worked, and it also supports special characters!

    One slight thing though;
    at the very beginning of my XML schema it says: <?xml version="1.0"?>, and under it <unapproved xmlns:sql="urn:schemas-microsoft-com:xml-sql">
    at the very beginning of excel XML file it says: <?xml version="1.0" encoding="UTF-8" standalone="true"?>

    Is there a way to change it to exactly correspond to my XML schema?

  25. #25
    Registered User
    Join Date
    04-20-2015
    Location
    Madrid
    MS-Off Ver
    2010
    Posts
    85

    Re: Creating an XML file with a specific schema

    Quote Originally Posted by regresss View Post
    Sure. Fill



    with your elements (column names). Then you save the file as map.xsd. It is important that the order of elements is same as order of columns in your worksheet. Once you have the add-in installed in Excel, click on Source, choose the xsd file, right click on the map in Excel, choose "Map Element", highlight your table with the header, click ok and export as xml file. Then you have you file.



    Basically, create a worksheet with data as at http://bitwizards.com/blog/november-...rksheet-to-xml and follow all the steps. Reproduce what they have done. Once you manage to do that, you can start modifying the xsd and apply it on your data.
    Also regress,
    The map.xsd has "NewsItemRow" and "NewsItemTable" instead of "Products".
    When I tried to change them to "Products", it gave me an error while trying to load the map in Excel: "Cannot load the specified XML or schema source".

  26. #26
    Registered User
    Join Date
    05-29-2015
    Location
    Germany
    MS-Off Ver
    2010
    Posts
    27

    Re: Creating an XML file with a specific schema

    Quote Originally Posted by papusale View Post
    Thanks regress! This actually worked, and it also supports special characters!

    One slight thing though;
    at the very beginning of my XML schema it says: <?xml version="1.0"?>, and under it <unapproved xmlns:sql="urn:schemas-microsoft-com:xml-sql">
    at the very beginning of excel XML file it says: <?xml version="1.0" encoding="UTF-8" standalone="true"?>

    Is there a way to change it to exactly correspond to my XML schema?
    Open the xml file in notepad and simply replace the lines (or even better, insert the lines in your xsd).


    Quote Originally Posted by papusale View Post
    Also regress,
    The map.xsd has "NewsItemRow" and "NewsItemTable" instead of "Products".
    When I tried to change them to "Products", it gave me an error while trying to load the map in Excel: "Cannot load the specified XML or schema source".
    Download an XML editor and modify the name of NewsItemRow element in definition, meaning the change will be applied on the whole document (NewsItemTable can be amended similarly or as described above, in notepad, since those are only two lines).

    Technically, you should be able to use Product and Producs in the xsd file, but it somehow doesn't work with the definition from http://bitwizards.com/blog/november-...rksheet-to-xml. I have an xsd at my workplace with which it works, but I don't have access to it right now. If you google other definitions, you will surely come across a solution. As I said above though, you can amend it in every XML editor as well.
    Last edited by regresss; 05-30-2015 at 09:57 AM.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. [SOLVED] Problem in Creating an XML Mapping Schema using Excel 2010
    By Alain-79 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 08-08-2013, 01:29 AM
  2. XMLl file - Mapping / Schema questions
    By Scott K in forum Excel General
    Replies: 0
    Last Post: 07-19-2013, 07:46 PM
  3. Creating sequential numbers & save to specific file location
    By ajaaay in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 07-22-2012, 06:19 PM
  4. Can not set up schema.ini file correctly...
    By ajocius1 in forum Excel General
    Replies: 0
    Last Post: 05-12-2009, 01:54 PM
  5. Replies: 0
    Last Post: 02-15-2005, 11:13 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1