Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
字符串介于两者之间的数组的自定义XML封送处理_Xml_Go - Fatal编程技术网

字符串介于两者之间的数组的自定义XML封送处理

字符串介于两者之间的数组的自定义XML封送处理,xml,go,Xml,Go,我需要读写一段XML。它是一个数组,除最后一个实体外,每个数组之间都有一个 <conditions>

我需要读写一段XML。它是一个
数组,除最后一个实体外,每个数组之间都有一个

<conditions>                                                                                                                                                                                                                                                                              
    <condition>                                                                                                                                                                                                                                                                           
      <label>a</label>                                                                                                                                                                                                                                                                    
    </condition>                                                                                                                                                                                                                                                                          
    <operator>AND</operator>                                                                                                                                                                                                                                                              
    <condition>                                                                                                                                                                                                                                                                           
      <label>b</label>                                                                                                                                                                                                                                                                    
    </condition>                                                                                                                                                                                                                                                                          
    <operator>AND</operator>                                                                                                                                                                                                                                                              
    <condition>                                                                                                                                                                                                                                                                           
      <label>c</label>                                                                                                                                                                                                                                                                    
    </condition>                                                                                                                                                                                                                                                                          
<conditions>
如果我封送结构,操作符只在底部出现一次。果然

<Conditions>
  <condition>
    <label>a</label>
  </condition>
  <condition>
    <label>b</label>
  </condition>
  <condition>
    <label>c</label>
  </condition>
  <operator>AND</operator>
</Conditions>

A.
B
C
及
如何使操作符在除最后一个之外的每个条件后都出现

我能得到的最接近的方法是使用包装器

func (c Conditions) MarshalXML(e *xml.Encoder, start xml.StartElement) error {                                                                                                                                                                                                            

    type tCondition struct {                                                                                                                                                                                                                                                              
        XMLName xml.Name `xml:"condition" json:"-"`                                                                                                                                                                                                                                       
        Label   string   `xml:"label"`                                                                                                                                                                                                                                                    
    }                                                                                                                                                                                                                                                                                     
    type tConditionWithOp struct {                                                                                                                                                                                                                                                        
        XMLName   xml.Name   `xml:"-" json:"-"`                                                                                                                                                                                                                                           
        Condition tCondition `xml: "conditions"`                                                                                                                                                                                                                                          
        Operator  string     `xml:",omitempty"`                                                                                                                                                                                                                                           
    }                                                                                                                                                                                                                                                                                     
    type tWrapper struct {                                                                                                                                                                                                                                                                
        OPS []tConditionWithOp                                                                                                                                                                                                                                                            
    }                                                                                                                                                                                                                                                                                     

    lst := make([]tConditionWithOp, 0, 10)                                                                                                                                                                                                                                                

    for idx, cond := range c.ConditionList {                                                                                                                                                                                                                                              
        tCond := tCondition{                                                                                                                                                                                                                                                              
            Label: cond.Label,                                                                                                                                                                                                                                                            
        }                                                                                                                                                                                                                                                                                 
        tCondOp := tConditionWithOp{                                                                                                                                                                                                                                                      
            Condition: tCond,                                                                                                                                                                                                                                                             
        }                                                                                                                                                                                                                                                                                 

        if idx < len(c.ConditionList)-1 {                                                                                                                                                                                                                                                 
            tCondOp.Operator = c.Operator                                                                                                                                                                                                                                                 
        }                                                                                                                                                                                                                                                                                 

        lst = append(lst, tCondOp)                                                                                                                                                                                                                                                        

    }                                                                                                                                                                                                                                                                                     
    wrapper := tWrapper{                                                                                                                                                                                                                                                                  
        OPS: lst,                                                                                                                                                                                                                                                                         
    }                                                                                                                                                                                                                                                                                     

    return e.EncodeElement(wrapper, start)                                                                                                                                                                                                                                                
}  
func(c条件)封送xml(e*xml.Encoder,start xml.StartElement)错误{
类型tCondition结构{
XMLName xml.Name`xml:“条件”json:“-”`
标签字符串`xml:“标签”`
}                                                                                                                                                                                                                                                                                     
类型tConditionWithOp结构{
XMLName xml.Name`xml:“-”json:“-”`
条件条件`xml:“条件”`
运算符字符串`xml:'',省略为空`
}                                                                                                                                                                                                                                                                                     
类型tWrapper结构{
OPS[]t有Op的条件
}                                                                                                                                                                                                                                                                                     
lst:=make([]t条件带OP,0,10)
对于idx,cond:=范围c.ConditionList{
tCond:=t条件{
标签:条件标签,
}                                                                                                                                                                                                                                                                                 
tCondOp:=tConditionWithOp{
条件:tCond,
func (c Conditions) MarshalXML(e *xml.Encoder, start xml.StartElement) error {                                                                                                                                                                                                            

    type tCondition struct {                                                                                                                                                                                                                                                              
        XMLName xml.Name `xml:"condition" json:"-"`                                                                                                                                                                                                                                       
        Label   string   `xml:"label"`                                                                                                                                                                                                                                                    
    }                                                                                                                                                                                                                                                                                     
    type tConditionWithOp struct {                                                                                                                                                                                                                                                        
        XMLName   xml.Name   `xml:"-" json:"-"`                                                                                                                                                                                                                                           
        Condition tCondition `xml: "conditions"`                                                                                                                                                                                                                                          
        Operator  string     `xml:",omitempty"`                                                                                                                                                                                                                                           
    }                                                                                                                                                                                                                                                                                     
    type tWrapper struct {                                                                                                                                                                                                                                                                
        OPS []tConditionWithOp                                                                                                                                                                                                                                                            
    }                                                                                                                                                                                                                                                                                     

    lst := make([]tConditionWithOp, 0, 10)                                                                                                                                                                                                                                                

    for idx, cond := range c.ConditionList {                                                                                                                                                                                                                                              
        tCond := tCondition{                                                                                                                                                                                                                                                              
            Label: cond.Label,                                                                                                                                                                                                                                                            
        }                                                                                                                                                                                                                                                                                 
        tCondOp := tConditionWithOp{                                                                                                                                                                                                                                                      
            Condition: tCond,                                                                                                                                                                                                                                                             
        }                                                                                                                                                                                                                                                                                 

        if idx < len(c.ConditionList)-1 {                                                                                                                                                                                                                                                 
            tCondOp.Operator = c.Operator                                                                                                                                                                                                                                                 
        }                                                                                                                                                                                                                                                                                 

        lst = append(lst, tCondOp)                                                                                                                                                                                                                                                        

    }                                                                                                                                                                                                                                                                                     
    wrapper := tWrapper{                                                                                                                                                                                                                                                                  
        OPS: lst,                                                                                                                                                                                                                                                                         
    }                                                                                                                                                                                                                                                                                     

    return e.EncodeElement(wrapper, start)                                                                                                                                                                                                                                                
}  
<Conditions>
  <OPS>
    <condition>
      <label>a</label>
    </condition>
    <Operator>AND</Operator>
  </OPS>
  <OPS>
    <condition>
      <label>b</label>
    </condition>
    <Operator>AND</Operator>
  </OPS>
  <OPS>
    <condition>
      <label>c</label>
    </condition>
  </OPS>
</Conditions>
type Operator struct {                                                                                                                                       
    Name string                                                                                                                                              
}                                                                                                                                                            

func (op Operator) MarshalXML(e *xml.Encoder, start xml.StartElement) error {                                                                                
    start.Name.Local = "operator"                                                                                                                            
    return e.EncodeElement(op.Name, start)                                                                                                                   
}                                                                                                                                                            

func (c Conditions) MarshalXML(e *xml.Encoder, start xml.StartElement) error {                                                                               

    start.Name.Local = "conditions"                                                                                                                          
    var arr []interface{}                                                                                                                                    

    for idx, cond := range c.ConditionList {                                                                                                                 
        if idx > 0 {                                                                                                                                         
            arr = append(arr, Operator{Name: c.Operator})                                                                                                    
        }                                                                                                                                                    
        arr = append(arr, cond)                                                                                                                              
    }                                                                                                                                                        

    type root struct {                                                                                                                                       
        ARR []interface{}                                                                                                                                    
    }                                                                                                                                                        

    return e.EncodeElement(root{ARR: arr}, start)                                                                                                            
}