亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

正確編組和解組包含 xmlns:wcm 和 xmlns:xsi 的 XML

正確編組和解組包含 xmlns:wcm 和 xmlns:xsi 的 XML

Go
炎炎設計 2023-03-07 17:31:50
我正在嘗試處理 Windows Autounattend.xml 文件的讀寫,以便創建和修改它們。我無法獲取xmlns:wcm和xmlns:xsi正確編組和解組屬性。我正在嘗試處理 Windows Autounattend.xml 文件的讀寫,以便創建和修改它們。我無法獲取xmlns:wcm和xmlns:xsi正確編組和解組屬性。
查看完整描述

1 回答

?
眼眸繁星

TA貢獻1873條經驗 獲得超9個贊

xmlns:wcmxmlns:xsi不是真正的屬性,它們是經過特殊處理的。您的 XML 既沒有元素也沒有來自wcmxsi模式的屬性,它們將被刪除。為什么你的代碼中需要它們?

不過,如果您真的非常需要它們,您可以將以下字段添加到結構中Component

    Attr                  []xml.Attr `xml:",any,attr"`

xml.Unmarshal文件說

如果 XML 元素具有先前規則未處理的屬性,并且結構具有包含“,any,attr”的關聯標記的字段,則 Unmarshal 會在第一個此類字段中記錄屬性值。

這是一個例子:https://go.dev/play/p/tGDh5Ay1kZW

func main() {

    var u Unattend

    err := xml.Unmarshal([]byte(document), &u)

    if err != nil {

        log.Fatal(err)

    }

    for _, a := range u.Settings[0].Components[0].Attr {

        fmt.Printf("Unmatched attributes: %s:%s = %s\n", a.Name.Space, a.Name.Local, a.Value)

    }

}

輸出:


Unmatched attributes: xmlns:wcm = http://schemas.microsoft.com/WMIConfig/2002/State

Unmatched attributes: xmlns:xsi = http://www.w3.org/2001/XMLSchema-instance


注意 1. 這個解決方案不太好的地方:它只處理額外的屬性<component/>。但是xmlns:XXX屬性可以出現在任何元素中。在一般情況下,您應該Attr在數據模型的所有結構中添加數組。

注2. 屬性xmlns:wcmxmlns:xsi不承擔任何業務邏輯。他們沒有提供有關設置和組件的信息。你真的需要它們嗎?

注 3. 絕對沒有要求命名這些屬性xmlns:wcmxmlns:xsi. 這只是一種常見的做法。XML 文檔可以命名它們xmlns:foo并且xmlns:bar- 它是完全有效的。連價值觀"http://schemas.microsoft.com/WMIConfig/2002/State"都沒"http://www.w3.org/2001/XMLSchema-instance"有意義。它們可以是任何字符串,唯一的要求是匹配URI 格式。例如urn:microsoft:wmi-config:2002:statehttp://schemas.microsoft.com/WMIConfig/2002/State

解析這些屬性Component非常具體,可能會在其他有效的 XML 文檔上失敗,這些文檔在其他元素中或使用其他后綴或值聲明這些屬性。

更新

編組工作不如預期。xml.Marshal專門處理xmlns名稱空間并轉換Attr為名稱空間xmlns:wcm并轉換xmlns:xsi為名稱空間_xmlns

<component name="Microsoft-Windows-Security-SPP" processorArchitecture="amd64" publicKeyToken="31bf3856ad364e35" language="neutral" versionScope="nonSxS" xmlns:_xmlns="xmlns" _xmlns:wcm="http://schemas.microsoft.com/WMIConfig/2002/State" _xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">

...

</component>

解決方法是為屬性使用特殊類型:


type XMLNSAttr struct {

    Name  xml.Name

    Value string

}


func (a XMLNSAttr) Label() string {

    if a.Name.Space == "" {

        return a.Name.Local

    }

    if a.Name.Local == "" {

        return a.Name.Space

    }

    return a.Name.Space + ":" + a.Name.Local

}


func (a *XMLNSAttr) UnmarshalXMLAttr(attr xml.Attr) error {

    a.Name = attr.Name

    a.Value = attr.Value

    return nil

}


func (a XMLNSAttr) MarshalXMLAttr(name xml.Name) (xml.Attr, error) {

    return xml.Attr{

        Name: xml.Name{

            Space: "",

            Local: a.Label(),

        },

        Value: a.Value,

    }, nil

這種類型通過構造一個將命名空間嵌入到本地名稱中的屬性來達到目的。場

    Attr                  []xml.Attr `xml:",any,attr"`

正確解碼/編碼xmlns:xxx屬性

示例: https: //go.dev/play/p/VDofREl5nf1

輸出:

Unmatched attributes: xmlns:wcm = http://schemas.microsoft.com/WMIConfig/2002/State

Unmatched attributes: xmlns:xsi = http://www.w3.org/2001/XMLSchema-instance

<unattend>

  <settings pass="windowsPE">

    <component name="Microsoft-Windows-International-Core-WinPE" processorArchitecture="amd64" publicKeyToken="31bf3856ad364e35" language="neutral" versionScope="nonSxS" xmlns:wcm="http://schemas.microsoft.com/WMIConfig/2002/State" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">

        ...

    </component>

    ...

  </settings>

</unattend>


查看完整回答
反對 回復 2023-03-07
  • 1 回答
  • 0 關注
  • 229 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號