Author Topic: XML Challenge  (Read 4480 times)

Offline AIR

  • BASIC Developer
  • Posts: 932
  • Coder
XML Challenge
« on: December 13, 2018, 07:03:41 PM »
New Challenge!

Using the attached xml file, For each Food record:

  • Extract the Name of the food
  • Extract the information for the Sodium content
  • Print out the Name in Quotes, taking care to eliminate any trailing spaces, and the Sodium Content, in the format shown below

"Avocado Dip" has a sodium level of 210
"Bagels, New York Style" has a sodium level of 510
"Beef Frankfurter, Quarter Pound" has a sodium level of 1100
"Chicken Pot Pie" has a sodium level of 810
"Cole Slaw" has a sodium level of 15
"Eggs" has a sodium level of 65
"Hazelnut Spread" has a sodium level of 20
"Potato Chips" has a sodium level of 180
"Soy Patties, Grilled" has a sodium level of 420
"Truffles, Dark Chocolate" has a sodium level of 10


AIR.

Offline AIR

  • BASIC Developer
  • Posts: 932
  • Coder
Re: XML Challenge
« Reply #1 on: December 13, 2018, 07:08:58 PM »
MBC VERSION:

Code: Text
  1. $MODULE xml.inc
  2. $EXECON
  3.  
  4. raw tree as NODE, node as NODE
  5. dim name$,sodium$
  6.  
  7. tree = xmlLoad("sample.xml")
  8. node = xmlFind(tree,"food")
  9. while node
  10.     name$ = xmlGet$(node,"/name")
  11.     sodium$ = xmlGet$(node,"/sodium")
  12.     print enc$(trim$(name$)), " has a sodium level of ",sodium$
  13.     node = xmlNext(node,tree,"food")
  14. wend
  15.  
  16. xmlDelete(tree)
  17.  

OUTPUT:

[riveraa@MacDev ~/Projects/mbc] $ ./xmlchallenge
"Avocado Dip" has a sodium level of 210
"Bagels, New York Style" has a sodium level of 510
"Beef Frankfurter, Quarter Pound" has a sodium level of 1100
"Chicken Pot Pie" has a sodium level of 810
"Cole Slaw" has a sodium level of 15
"Eggs" has a sodium level of 65
"Hazelnut Spread" has a sodium level of 20
"Potato Chips" has a sodium level of 180
"Soy Patties, Grilled" has a sodium level of 420
"Truffles, Dark Chocolate" has a sodium level of 10
[riveraa@MacDev ~/Projects/mbc] $


AIR.

Offline John

  • Forum Support / SB Dev
  • Posts: 3598
    • ScriptBasic Open Source Project
Re: XML Challenge
« Reply #2 on: December 13, 2018, 08:34:51 PM »
Script BASIC

Code: ScriptBasic
  1. ' XML Chalenge - Script BASIC - JRS
  2.  
  3. IMPORT t.bas
  4.  
  5. SPLITA t::LoadString("sample.xml") BY "<food>" TO food
  6. FOR idx = 0 TO UBOUND(food)
  7.   IF food[idx] LIKE "*<name>*</name>*<sodium>*</sodium>*" THEN _
  8.   PRINT FORMAT("\"%s\" has a sodium level of %g\n", TRIM(JOKER(2)),JOKER(4))
  9. NEXT
  10.  


$ time scriba xmlcc
"Avocado Dip" has a sodium level of 210
"Bagels, New York Style" has a sodium level of 510
"Beef Frankfurter, Quarter Pound" has a sodium level of 1100
"Chicken Pot Pie" has a sodium level of 810
"Cole Slaw" has a sodium level of 15
"Eggs" has a sodium level of 65
"Hazelnut Spread" has a sodium level of 20
"Potato Chips" has a sodium level of 180
"Soy Patties, Grilled" has a sodium level of 420
"Truffles, Dark Chocolate" has a sodium level of 10

real   0m0.012s
user   0m0.012s
sys   0m0.000s
$
« Last Edit: December 13, 2018, 08:52:29 PM by John »

Offline John

  • Forum Support / SB Dev
  • Posts: 3598
    • ScriptBasic Open Source Project
Re: XML Challenge -OT
« Reply #3 on: December 13, 2018, 09:52:40 PM »
AIR,

If can you create a project for MBC in the sandbox, I'll create a MBC syntax highlighting file for the forum.
« Last Edit: December 13, 2018, 10:01:57 PM by John »

Offline AIR

  • BASIC Developer
  • Posts: 932
  • Coder
Re: XML Challenge
« Reply #4 on: December 13, 2018, 10:44:16 PM »
Script BASIC



Nice!.

If we're gonna brute-force it:

Code: Text
  1. $EXECON
  2.  
  3. dim a$, cnt, name as REGEX, sodium as REGEX
  4.  
  5. a$ = loadfile$("sample.xml")
  6. cnt = tally(a$,"<food>")
  7.  
  8. for int i = 0 to cnt-1
  9.     if regmatch(a$,"<name>([a-zA-Z ,]+)</name>",&name) and regmatch(a$,"<sodium>([0-9]+)</sodium>",&sodium) then
  10.         print enc$(trim$(name.results[1]$)), " has a sodium level of ",sodium.results[1]$
  11.     end if
  12.     a$ = REMAIN$(a$, "</food>")
  13. next

[riveraa@MacDev ~/Projects/mbc] $ time ./xmlChallenge2
"Avocado Dip" has a sodium level of 210
"Bagels, New York Style" has a sodium level of 510
"Beef Frankfurter, Quarter Pound" has a sodium level of 1100
"Chicken Pot Pie" has a sodium level of 810
"Cole Slaw" has a sodium level of 15
"Eggs" has a sodium level of 65
"Hazelnut Spread" has a sodium level of 20
"Potato Chips" has a sodium level of 180
"Soy Patties, Grilled" has a sodium level of 420
"Truffles, Dark Chocolate" has a sodium level of 10

real   0m0.005s
user   0m0.002s
sys   0m0.002s
[riveraa@MacDev ~/Projects/mbc] $


AIR.

Offline petelomax

  • BASIC Developer
  • Posts: 9
  • Author of Phix
    • The Phix Programming Language
Re: XML Challenge
« Reply #5 on: December 17, 2018, 09:35:13 AM »
Phix

Code: [Select]
include builtins\xml.e -- (needs 0.8.0+)

string xml_text = get_text("C:\\Users\\Pete\\sample.xml")

sequence xml = xml_parse(xml_text)

sequence foods = get_xml_nodes(xml[XML_CONTENTS],"food")
for i=1 to length(foods) do
    string name = get_xml_nodes(foods[i],"name")[1][XML_CONTENTS],
           sodium = get_xml_nodes(foods[i],"sodium")[1][XML_CONTENTS]
    printf(1,"%s has a sodium level of %s\n",{name,sodium})
end for

Output

Avocado Dip has a sodium level of 210
Bagels, New York Style  has a sodium level of 510
Beef Frankfurter, Quarter Pound  has a sodium level of 1100
Chicken Pot Pie has a sodium level of 810
Cole Slaw has a sodium level of 15
Eggs has a sodium level of 65
Hazelnut Spread has a sodium level of 20
Potato Chips has a sodium level of 180
Soy Patties, Grilled has a sodium level of 420
Truffles, Dark Chocolate has a sodium level of 10

Pete

Offline AIR

  • BASIC Developer
  • Posts: 932
  • Coder
Re: XML Challenge
« Reply #6 on: March 07, 2019, 01:09:29 AM »
GO VERSION

Code: Go
  1. package main
  2.  
  3. import (
  4.         "encoding/xml"
  5.         "fmt"
  6.         "io/ioutil"
  7.         "log"
  8. )
  9.  
  10. type Nutrition struct {
  11.         Food []struct {
  12.                 Name   string `xml:"name"`
  13.                 Sodium string `xml:"sodium"`
  14.         } `xml:"food"`
  15. }
  16.  
  17. func chkError(e error) {
  18.         if e != nil {
  19.                 log.Fatal(e)
  20.         }
  21. }
  22.  
  23. func main() {
  24.         var nutrition Nutrition
  25.  
  26.         xmlFile, err := ioutil.ReadFile("sample.xml")
  27.         chkError(err)
  28.  
  29.         err = xml.Unmarshal(xmlFile, &nutrition)
  30.         chkError(err)
  31.  
  32.         for _, food := range nutrition.Food {
  33.                 fmt.Printf("\"%s\" has a soduim level of %s\n",strings.TrimSpace(food.Name), food.Sodium)
  34.         }
  35. }
  36.  

OUTPUT:

"Avocado Dip" has a soduim level of 210
"Bagels, New York Style" has a soduim level of 510
"Beef Frankfurter, Quarter Pound" has a soduim level of 1100
"Chicken Pot Pie" has a soduim level of 810
"Cole Slaw" has a soduim level of 15
"Eggs" has a soduim level of 65
"Hazelnut Spread" has a soduim level of 20
"Potato Chips" has a soduim level of 180
"Soy Patties, Grilled" has a soduim level of 420
"Truffles, Dark Chocolate" has a soduim level of 10


AIR.

Offline AIR

  • BASIC Developer
  • Posts: 932
  • Coder
Re: XML Challenge
« Reply #7 on: August 14, 2021, 05:28:45 PM »
2 years later....... ;D

NIM
Code: Text
  1. import xmltree, xmlparser, strutils
  2.  
  3. let doc = loadXml("sample.xml")
  4.  
  5. for x in doc.findAll("food"):
  6.     echo "'$#' has a sodium level of $#" % [x.child("name").innerText, x.child("sodium").innerText]
  7.  

Code: [Select]
'Avocado Dip' has a sodium level of 210
'Bagels, New York Style ' has a sodium level of 510
'Beef Frankfurter, Quarter Pound ' has a sodium level of 1100
'Chicken Pot Pie' has a sodium level of 810
'Cole Slaw' has a sodium level of 15
'Eggs' has a sodium level of 65
'Hazelnut Spread' has a sodium level of 20
'Potato Chips' has a sodium level of 180
'Soy Patties, Grilled' has a sodium level of 420
'Truffles, Dark Chocolate' has a sodium level of 10
« Last Edit: August 14, 2021, 05:30:51 PM by AIR »