2015-05-17 145 views
0

我想根據用戶創建的Excel中的標題創建一個XML文件。我想通過一個真實的例子來解釋。用Excel創建一個XML

這是由用戶創建的excel格式。這個模板有時可能會改變,這就是爲什麼我試圖動態創建。

(細胞通過PIPE分隔和這些被放置在第一行中。)

SALES_ORDERS.ORDER_SLIP.NUMBER|SALES_ORDERS.ORDER_SLIP.DATE|SALES_ORDERS.ORDER_SLIP.ARP_CODE|SALES_ORDERS.ORDER_SLIP.NOTES1|SALES_ORDERS.ORDER_SLIP.TRANSACTIONS.TRANSACTION.TYPE 

這是我需要用Excel上面創建XML。

<SALES_ORDERS> 
    <ORDER_SLIP DBOP="INS" > 
     <NUMBER>...</NUMBER> 
     <DATE>...</DATE> 
     <ARP_CODE>...</ARP_CODE> 
     <NOTES1>...</NOTES1> 
     <TRANSACTIONS> 
      <TRANSACTION> 
       <TYPE>...</TYPE> 
       ......... 

任何人都可以幫助我使用此算法。

+1

從該示例中不清楚你將如何處理重複?如果有多個(例如)ORDER或TRANSACTION標籤會怎麼樣? –

+0

好問題,我忘了提及。如果有2-3條TRANSACTION線,那麼excel中會有2-3條線,與我在С上寫的Headar Information –

+0

相同。採取更新的答案 – splash58

回答

1

我很久沒寫C了。我不知道這是否有幫助。用PHP代碼:

$string = "SALES_ORDERS.ORDER_SLIP.NUMBER|SALES_ORDERS.ORDER_SLIP.DATE|SALES_ORDERS.ORDER_SLIP.ARP_CODE|SALES_ORDERS.ORDER_SLIP.NOTES1|SALES_ORDERS.ORDER_SLIP.TRANSACTIONS.TRANSACTION.TYPE"; 
// Split the string by | to an array of pathes 
    $arr = explode('|', $string); 
    $xml = ''; 
    foreach ($arr as $s) { 
// Split a string by the point char to an array of items 
     $path = explode('.', $s); 
// set the pointer to tho root of the xml array 
     $p = &$xml; 
// Walk along the path 
     foreach($path as $item) { 
// If this node does not exist 
      if (!isset($p[$item])) { 
// if this level does not exist (item parent is not an array), make it 
       if(!is_array($p)) $p = array(); 
       $p[$item] = ''; 
      } 
// move the pointer to this item 
      $p = &$p[$item]; 
     } 
    } 
    var_dump($xml); 

輸出:

array(1) { 
    ["SALES_ORDERS"]=> array(1) { 
     ["ORDER_SLIP"]=> array(5) { 
      ["NUMBER"]=> string(0) "" 
      ["DATE"]=> string(0) "" 
      ["ARP_CODE"]=> string(0) "" 
      ["NOTES1"]=> string(0) "" 
      ["TRANSACTIONS"]=> array(1) { 
       ["TRANSACTION"]=> array(1) { 
        ["TYPE"]=> &string(0) "" 
    } } } } } 

UPDATE根據缺乏響應,算法在PHP,你真的不明白。必須記住C.正確根據目前的趨勢:)

#include <iostream> 
using namespace std; 

#define PATH '|' 
#define NODE '.' 
#define EOS 0 
#define NULL 0 

class node { 
private: 
    char *name = NULL; 
    node *next = NULL, *down = NULL; 
    // data *void = NULL; 

public: 
    node(char *word = NULL) { 
     this->name = word; 
} 
    void echoNode(node *p, int level = 0) { 
     char *str; 
     int i; 
     if (!p) return; 
     i = level * 4; while (i--) cout << ' '; 
     cout << "<"; 
     str = p->name; while (*str) cout << *str++; 
     cout << ">"; 
     if (p->down) { 
      cout << "\n"; 
      echoNode(p->down, level + 1); 
      i = level * 4; while (i--) cout << ' '; 
     } 
     cout << "</"; 
     str = p->name; while (*str) cout << *str++; 
     cout << ">\n"; 
     if (p->next) echoNode(p->next, level); 
    } 

    node * FindChild(char *word) { 
     node *p = this->down; 
     while(p) if (!strcmp(p->name, word)) break; else p = p->next; 
     return p; 
    } 
    node * AddChild(char *word) { 
     node *p = this; 
     if(p->down) { 
      p = p->down; 
      while (p->next) p = p->next; 
      p->next = new node; 
      p = p->next; 
     } 
     else { 
      p->down = new node; 
      p = p->down; 
     } 
     p->name = word; 
     return p; 
    } 
}; 

void main() { 
    char str[] = "SALES_ORDERS.ORDER_SLIP.NUMBER|SALES_ORDERS.ORDER_SLIP.DATE|SALES_ORDERS.ORDER_SLIP.ARP_CODE|SALES_ORDERS.ORDER_SLIP.NOTES1|SALES_ORDERS.ORDER_SLIP.TRANSACTIONS.TRANSACTION.TYPE\x0"; 

    char *p = str; 
    char *word; 
    char status; 

    node *xml = new node("root"); 
    node *pxml, *t; 

    do { 
     pxml = xml; 
     do { 
      word = p; 
      while (*p && *p != PATH && *p != NODE) p++; 
      status = *p; 
      *p++ = EOS; 
      if (!(t = pxml->FindChild(word))) pxml = pxml->AddChild(word); 
      else pxml = t; 
     } while (status && status != PATH); 
    } while (status != EOS); 
    xml->echoNode(xml); 
}