How to get tag content (encoding.xml) ? #23103
Replies: 4 comments 1 reply
-
One can learn a lot by studying the library tests codes: https://github.com/vlang/v/tree/master/vlib/encoding/xml/test/local You can also provide your data file and code to get an idea of the problem. |
Beta Was this translation helpful? Give feedback.
-
I think what you get still is a sum type. Assume this is you <?xml version="1.0" encoding="UTF-8"?>
<fiche>
<id_fiche>8971</id_fiche>
</fiche> Then we parse the file until last node then do a match type import encoding.xml
doc := xml.XMLDocument.from_string('
<?xml version="1.0" encoding="UTF-8"?>
<fiche>
<id_fiche>8971</id_fiche>
</fiche>')!
for fiche in doc.root.get_elements_by_tag("fiche") {
nid_fiche := fiche.get_elements_by_tag("id_fiche")
val := nid_fiche[0].children[0]
println("sum type yet: ${val}") // sum type yet...
match val {
string {
println('particular string type: ${val}') // string value
}
else { // else is mandatory to exhaust the sum type (the rest of other 3 types)
}
}
} Result:
Acording to the docs,
You can find the code above here: https://play.vlang.io/p/f690e2db6d There you can test small codes within your browser, edit, click Run, click Share and paste the link. |
Beta Was this translation helpful? Give feedback.
-
Salut, Merci beaucoup !
Retour
So many question ?
retour
in this case i can't transtype nid_fiche[0].children[0] in string Thank's a lot 4 your help ! |
Beta Was this translation helpful? Give feedback.
-
Yes, match forces the variable to become other type inside the block. We programmers can't shadow variables, but V says hold my beer, I can. Look how you can separate strings from comments. And since a comment have a field named text you can use import encoding.xml
doc := xml.XMLDocument.from_string('
<?xml version="1.0" encoding="UTF-8"?>
<fiche>
<id_fiche>8971</id_fiche>
<id_fiche><!--This is a comment-->8972</id_fiche>
</fiche>')!
for fiche in doc.root.get_elements_by_tag("fiche") {
nid_fiche := fiche.get_elements_by_tag("id_fiche")
for f in nid_fiche {
for c in f.children {
match c {
string { println('string: ${c}') }
xml.XMLComment { println('comment: ${c.text}')}
else { }
}
}
}
} Result:
|
Beta Was this translation helpful? Give feedback.
-
Hello,
8971I'm stuck ,i'm totally noob in V
i can't access the content of the tags with encoding.xml
for fiche in doc.root.get_elements_by_tag("fiche") {
nid_fiche := fiche.get_elements_by_tag("id_fiche")
val := nid_fiche[0].children[0]
print("IDFiche ${val}")
}
i get
IDFiche xml.XMLNodeContents('8971')
but i cant acces the content with .text or .content
and it's driving me crazy.
Beta Was this translation helpful? Give feedback.
All reactions