简体   繁体   English

使用宏使用excel中的数据创建xml

[英]create xml using data from excel using macro

Here is my requirement - 1) I have an xml and corresponding xsd document 2) It's a complex xml with multiple elements and their hierarchy. 这是我的要求-1)我有一个xml和相应的xsd文档2)这是一个包含多个元素及其层次结构的复杂xml。 But there are only certain elements/fields that are important 3) I want to create an excel with these fields where user can enter data across multiple rows 4) When user clicks on create xml, separate xml should be created for each row 5) The xml created should contain all the elements (user entered + many more in original xml) 但是只有某些元素/字段很重要3)我想用这些字段创建一个excel,用户可以在其中跨多个行输入数据4)当用户单击create xml时,应为每行创建单独的xml 5)创建的xml应该包含所有元素(用户输入的内容以及原始xml中的更多内容)

This is just a Sample- 这只是一个示例

<A>
   <A1/>
   <A2/>
</A>
<B>
   <B1>*User entered value*</B1>
   <B2>*User entered value*</B2>
</B>
<C>
   <C1/>
   <C2/>
</C>

I found some references on MSXML2.DOMDocument60 where one can create xml using this API and also validate against schema through excel macro. 我在MSXML2.DOMDocument60上找到了一些参考,其中可以使用此API创建xml,也可以通过excel宏针对架构进行验证。 But in my case there are 1000s of tags. 但就我而言,有数千个标签。 Below are some options I have in mind- 以下是我想到的一些选项-

  1. Use DOMDocument60 API and construct the xml line by line using macro. 使用DOMDocument60 API并使用宏逐行构造xml。 Since there are 1000s of tags, this approach would gulp 1000s of lines of code and that would make maintenance a cumbersome task ! 由于有1000个标签,因此这种方法将吞噬1000行代码,这将使维护成为一项繁琐的任务!
  2. Since I have sample xml, I can empty the existing values and then update the xml using the user entered value (get from excel cell data) through macro. 由于我有示例xml,因此我可以清空现有值,然后使用用户输入的值(从excel单元格数据获取)通过宏更新xml。 This seems more effective as it would save 1000s of lines of code, but is this possible ? 这似乎更有效,因为它将节省数千行代码,但这可能吗? Since the elements that needs to be updated are spread across xml, will I be able to search for an element and update only that element and not impact any other data/tag in the xml ? 由于需要更新的元素分布在整个xml中,我将能够搜索一个元素并仅更新该元素,而不会影响xml中的任何其他数据/标签?
  3. Can anything be done with xsd ? xsd可以做任何事情吗? None flashes to my mind with my requirement 我的要求没有一个闪过我的脑海
  4. Any other option ? 还有其他选择吗?

Appreciate any help on this. 感谢对此的任何帮助。 Thanks ! 谢谢 !

If you have a base, you can use something like that: 如果您有基础,则可以使用类似的方法:

Open "e:\0\xml.xml" For Input As #1
Open "e:\0\xml1.xml" For Output As #2

i = 1
While Not EOF(1)
    Line Input #1, aa

    If InStr(1, aa, "<B1>", vbTextCompare) > 0 Then
        aa = Left(aa, 3 + InStr(1, aa, "<B1>", vbTextCompare)) & Cells(i, 1).Value & Right(aa, Len(aa) - InStr(1, aa, "</B1>", vbTextCompare) + 1)
        i = i + 1
    End If

    Print #2, aa
Wend

Close #1
Close #2

the code copy the original file, changing the text inside the tag B1 with the values in column A. 代码复制原始文件,使用A列中的值更改标签B1中的文本。
If you have more "pieces", you can merge different files... 如果您有更多“片段”,则可以合并不同的文件...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM