2015-03-31 30 views
1

將php腳本移植到golang時出現問題。在golang中收集具有給定名稱的所有xml結構體

是否有golang類似PHP的任何方法:

$processRefList = $faceContainer->getElementsByTagName('processRef');

有一個名爲processRef嵌套元素的數量龐大的XML文件。

所以我需要收集所有這些元素而不處理嵌套結構樹。

這裏XML的例子部分:

<face> 
<processes> 
    <process group="1" type="regular"/> 
    <process group="2" type="spotUV" state="off"/> 
</processes> 
<assets>...</assets> 
<groups> 
    <group id="1"> 
     <assetRefs> 
      <imageRef ref="1" x="0" y="0" id="8"> 
       <processRefs> 
        <!-- This applies spotUV to this asset and removes the regular process. --> 
        <processRef ref="1" state="off"/> 
        <processRef ref="2" state="on"/> 
       </processRefs> 
      </imageRef> 
      <textRef ref="1" x="161" y="41" id="7"/> 
      <textRef ref="2" x="160" y="55" id="6"> 
       <processRefs> 
        <!-- This applies spotUV to this asset and also leaves regular process applied. --> 
        <processRef ref="2" state="on"/> 
       </processRefs> 
      </textRef> 
     </assetRefs> 
    </group> 
    <group id="2">...</group> 
</groups> 

+0

這可能會有幫助。 http://blog.labix.org/2013/06/07/efficient-xpath-for-go – 2015-03-31 15:25:56

回答

-3

試試這個

$extractvar = new DOMDocument(); 
$extractvar ->load("yourfile"); 

$itemextract= $extractvar ->getElementsByTagName("faceContainerRefs"); 

foreach($itemextract as $value) 
{ 
$var1= $value->getElementsByTagName("faceContainerRef "); 
$var= $var1->item(0)->nodeValue; 
} 
+0

可能你沒有正確理解我。我試圖找到php功能的golang實現。但無論如何感謝您的回答! – 2015-03-31 15:16:15

-1
////store all xml data in var response 
type XMLQuery struct { 
Loc string `xml:",chardata"` 
} 
var l XMLQuery 
decoder := xml.NewDecoder(strings.NewReader(string(response))) //store 
all xml data in var response 
for { 

token, _ := decoder.Token() 

if token == nil { 
    break 
} 

switch Element := token.(type) { 
case xml.StartElement: 
    if Element.Name.Local == "processRef" { 
    fmt.Println("Element name is : ", Element.Name.Local) 

    err := decoder.DecodeElement(&l, &Element) 
    if err != nil { 
     fmt.Println(err) 
    } 

    attrVal :=l.Loc 
    fmt.Println("Title: ", attrVal) 

}}} 
+0

您可能想要爲您的答案添加一些上下文! – Daniel 2017-05-16 00:02:38

相關問題