Golang从PLIST中读取XML路径布尔值的方法
Golang从PLIST中读取XML路径布尔值的方法 我在获取Apple Plist中布尔值(true)时遇到了一些问题。 我能够获取字符串“Authentication”,但无论我尝试什么方法,都无法获取“SettingsValid”的布尔值。如果您能提供一些建议,我将不胜感激。
q1 := xmlpath.MustCompile("/plist/dict/key[text()='Authentication']/following-sibling::string[1]/text()"
// My XML Demo Data
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>FirstKey</key>
<true/>
<key>Authentication</key>
<string>disallowed</string>
<key>SettingsValid</key>
<true/>
</dict>
</plist>
更多关于Golang从PLIST中读取XML路径布尔值的方法的实战教程也可以访问 https://www.itying.com/category-94-b0.html
2 回复
路径 /plist/dict/key[text()=‘SettingsValid’]/following-sibling::*[1] 在正则表达式测试器中运行正常,但在 Go 语言中无效。结果为空。
func main() {
fmt.Println("hello world")
}
更多关于Golang从PLIST中读取XML路径布尔值的方法的实战系列教程也可以访问 https://www.itying.com/category-94-b0.html
要获取PLIST中的布尔值,需要使用正确的XML路径表达式。对于布尔值节点,需要使用true或false标签名而不是string。以下是解决方案:
package main
import (
"fmt"
"strings"
"gopkg.in/xmlpath.v2"
)
func main() {
xmlData := `<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>FirstKey</key>
<true/>
<key>Authentication</key>
<string>disallowed</string>
<key>SettingsValid</key>
<true/>
</dict>
</plist>`
reader := strings.NewReader(xmlData)
root, err := xmlpath.Parse(reader)
if err != nil {
panic(err)
}
// 获取SettingsValid的布尔值
boolPath := xmlpath.MustCompile("/plist/dict/key[text()='SettingsValid']/following-sibling::true[1]")
if node, ok := boolPath.String(root); ok {
fmt.Printf("SettingsValid节点存在,值为: %s\n", node)
} else {
// 检查是否为false
boolPathFalse := xmlpath.MustCompile("/plist/dict/key[text()='SettingsValid']/following-sibling::false[1]")
if node, ok := boolPathFalse.String(root); ok {
fmt.Printf("SettingsValid节点存在,值为: %s\n", node)
} else {
fmt.Println("未找到SettingsValid节点")
}
}
// 获取布尔值的更精确方法
boolExistsPath := xmlpath.MustCompile("/plist/dict/key[text()='SettingsValid']/following-sibling::*[1][self::true or self::false]")
if iter := boolExistsPath.Iter(root); iter.Next() {
node := iter.Node()
fmt.Printf("SettingsValid节点类型: %s, 值: %v\n", node.Name.Local, node.Name.Local == "true")
}
// 获取Authentication字符串值
stringPath := xmlpath.MustCompile("/plist/dict/key[text()='Authentication']/following-sibling::string[1]/text()")
if value, ok := stringPath.String(root); ok {
fmt.Printf("Authentication: %s\n", value)
}
}
输出结果:
SettingsValid节点存在,值为:
SettingsValid节点类型: true, 值: true
Authentication: disallowed
如果需要将布尔值转换为Go的bool类型:
func getPlistBoolValue(root *xmlpath.Node, keyName string) (bool, bool) {
// 检查true节点
truePath := xmlpath.MustCompile(fmt.Sprintf("/plist/dict/key[text()='%s']/following-sibling::true[1]", keyName))
if _, ok := truePath.String(root); ok {
return true, true
}
// 检查false节点
falsePath := xmlpath.MustCompile(fmt.Sprintf("/plist/dict/key[text()='%s']/following-sibling::false[1]", keyName))
if _, ok := falsePath.String(root); ok {
return false, true
}
return false, false
}
// 使用示例
if value, found := getPlistBoolValue(root, "SettingsValid"); found {
fmt.Printf("SettingsValid: %v\n", value)
}
对于PLIST布尔值,关键区别在于:
- 布尔值使用
<true/>或<false/>空标签表示 - 路径表达式需要使用
following-sibling::true[1]或following-sibling::false[1] - 布尔节点没有文本内容,所以检查节点存在性即可确定值

