Added some more libxml2 examples to Fun. (0.35.1)
This commit is contained in:
parent
e65756226d
commit
db450a871c
8 changed files with 218 additions and 1 deletions
|
|
@ -1,5 +1,5 @@
|
|||
cmake_minimum_required(VERSION 3.10)
|
||||
project(fun VERSION 0.35.0 LANGUAGES C)
|
||||
project(fun VERSION 0.35.1 LANGUAGES C)
|
||||
|
||||
set(CMAKE_C_STANDARD 99)
|
||||
set(CMAKE_C_STANDARD_REQUIRED ON)
|
||||
|
|
|
|||
32
examples/data/catalog.xml
Normal file
32
examples/data/catalog.xml
Normal file
|
|
@ -0,0 +1,32 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<catalog>
|
||||
<product id="SKU-1001">
|
||||
<name>Wireless Keyboard</name>
|
||||
<category>Peripherals</category>
|
||||
<price currency="USD">39.99</price>
|
||||
<specs>
|
||||
<layout>US</layout>
|
||||
<connection>Bluetooth</connection>
|
||||
<battery>AA</battery>
|
||||
</specs>
|
||||
</product>
|
||||
<product id="SKU-2002">
|
||||
<name>27" Monitor</name>
|
||||
<category>Displays</category>
|
||||
<price currency="USD">199.00</price>
|
||||
<specs>
|
||||
<resolution>2560x1440</resolution>
|
||||
<panel>IPS</panel>
|
||||
<refresh>75Hz</refresh>
|
||||
</specs>
|
||||
</product>
|
||||
<product id="SKU-3003">
|
||||
<name>USB-C Dock</name>
|
||||
<category>Peripherals</category>
|
||||
<price currency="USD">89.50</price>
|
||||
<specs>
|
||||
<ports>2xHDMI, 3xUSB-A, 1xUSB-C PD</ports>
|
||||
<pd>65W</pd>
|
||||
</specs>
|
||||
</product>
|
||||
</catalog>
|
||||
22
examples/data/employees.xml
Normal file
22
examples/data/employees.xml
Normal file
|
|
@ -0,0 +1,22 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<company>
|
||||
<department name="Engineering">
|
||||
<employee id="E-100">
|
||||
<name>Alice Doe</name>
|
||||
<role>Senior Developer</role>
|
||||
<email>alice@example.com</email>
|
||||
</employee>
|
||||
<employee id="E-101">
|
||||
<name>Bob Roe</name>
|
||||
<role>DevOps Engineer</role>
|
||||
<email>bob@example.com</email>
|
||||
</employee>
|
||||
</department>
|
||||
<department name="Sales">
|
||||
<employee id="S-200">
|
||||
<name>Carol Smith</name>
|
||||
<role>Account Executive</role>
|
||||
<email>carol@example.com</email>
|
||||
</employee>
|
||||
</department>
|
||||
</company>
|
||||
11
examples/data/ns_example.xml
Normal file
11
examples/data/ns_example.xml
Normal file
|
|
@ -0,0 +1,11 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<ns:library xmlns:ns="http://example.org/ns/library" xmlns:bk="http://example.org/ns/book">
|
||||
<bk:book id="B-1">
|
||||
<bk:title>The Art of Fun</bk:title>
|
||||
<bk:author>J. Findeisen</bk:author>
|
||||
</bk:book>
|
||||
<bk:book id="B-2">
|
||||
<bk:title>Minimal VM Design</bk:title>
|
||||
<bk:author>A. Dev</bk:author>
|
||||
</bk:book>
|
||||
</ns:library>
|
||||
52
examples/xml_access_catalog.fun
Normal file
52
examples/xml_access_catalog.fun
Normal file
|
|
@ -0,0 +1,52 @@
|
|||
/*
|
||||
* This file is part of the Fun programming language.
|
||||
* https://fun-lang.xyz/
|
||||
*
|
||||
* Copyright 2025 Johannes Findeisen <you@hanez.org>
|
||||
* Licensed under the terms of the Apache-2.0 license.
|
||||
* https://opensource.org/license/apache-2-0
|
||||
*
|
||||
* Added: 2025-12-09
|
||||
*/
|
||||
|
||||
/*
|
||||
* Demonstrate reading specific fields from an XML file using
|
||||
* the minimal XML API (root name) and simple string parsing.
|
||||
*/
|
||||
|
||||
include <io/xml.fun>
|
||||
|
||||
path = "./examples/data/catalog.xml"
|
||||
xml = XML()
|
||||
doc = xml.from_file(path)
|
||||
if (doc == 0)
|
||||
print("Failed to load: ")
|
||||
print(path)
|
||||
else
|
||||
root = xml.root(doc)
|
||||
print("Root element: ")
|
||||
print(xml.name(root))
|
||||
|
||||
// Show how to access specific fields by quick-and-dirty parsing
|
||||
content = read_file(path) // raw XML text
|
||||
// First product name
|
||||
name = xml.between(content, "<name>", "</name>")
|
||||
// First price value and its currency attribute (extract value, then attribute)
|
||||
price_val = xml.between(content, "<price", "</price>")
|
||||
currency = ""
|
||||
if (len(price_val) > 0)
|
||||
currency = xml.between(price_val, "currency=\"", "\"")
|
||||
// strip attribute tag part
|
||||
price_text = xml.between(price_val, ">", "") // until end
|
||||
if (len(price_text) == 0)
|
||||
price_text = price_val
|
||||
else
|
||||
price_text = ""
|
||||
|
||||
print("First product name: ")
|
||||
print(name)
|
||||
print("First price: ")
|
||||
if (len(currency) > 0)
|
||||
print(currency)
|
||||
print(" ")
|
||||
print(price_text)
|
||||
44
examples/xml_access_employees.fun
Normal file
44
examples/xml_access_employees.fun
Normal file
|
|
@ -0,0 +1,44 @@
|
|||
/*
|
||||
* This file is part of the Fun programming language.
|
||||
* https://fun-lang.xyz/
|
||||
*
|
||||
* Copyright 2025 Johannes Findeisen <you@hanez.org>
|
||||
* Licensed under the terms of the Apache-2.0 license.
|
||||
* https://opensource.org/license/apache-2-0
|
||||
*
|
||||
* Added: 2025-12-09
|
||||
*/
|
||||
|
||||
/*
|
||||
* Access selected fields in employees.xml using minimal XML API.
|
||||
*/
|
||||
|
||||
include <io/xml.fun>
|
||||
|
||||
path = "./examples/data/employees.xml"
|
||||
xml = XML()
|
||||
doc = xml.from_file(path)
|
||||
if (doc == 0)
|
||||
print("Failed to load: ")
|
||||
print(path)
|
||||
else
|
||||
root = xml.root(doc)
|
||||
print("Root element: ")
|
||||
print(xml.name(root))
|
||||
|
||||
content = read_file(path)
|
||||
// Find the first <employee> block and extract its fields
|
||||
first_emp = xml.between(content, "<employee", "</employee>")
|
||||
name = xml.between(first_emp, "<name>", "</name>")
|
||||
role = xml.between(first_emp, "<role>", "</role>")
|
||||
email = xml.between(first_emp, "<email>", "</email>")
|
||||
emp_id = xml.between(first_emp, "id=\"", "\"")
|
||||
|
||||
print("First employee id: ")
|
||||
print(emp_id)
|
||||
print("Name: ")
|
||||
print(name)
|
||||
print("Role: ")
|
||||
print(role)
|
||||
print("Email: ")
|
||||
print(email)
|
||||
38
examples/xml_access_ns.fun
Normal file
38
examples/xml_access_ns.fun
Normal file
|
|
@ -0,0 +1,38 @@
|
|||
/*
|
||||
* This file is part of the Fun programming language.
|
||||
* https://fun-lang.xyz/
|
||||
*
|
||||
* Copyright 2025 Johannes Findeisen <you@hanez.org>
|
||||
* Licensed under the terms of the Apache-2.0 license.
|
||||
* https://opensource.org/license/apache-2-0
|
||||
*
|
||||
* Added: 2025-12-09
|
||||
*/
|
||||
|
||||
/*
|
||||
* Access a namespaced XML file and show prefixed element names.
|
||||
*/
|
||||
|
||||
include <io/xml.fun>
|
||||
|
||||
path = "./examples/data/ns_example.xml"
|
||||
xml = XML()
|
||||
doc = xml.from_file(path)
|
||||
if (doc == 0)
|
||||
print("Failed to load: ")
|
||||
print(path)
|
||||
else
|
||||
root = xml.root(doc)
|
||||
// With namespaces, the node name may include the prefix, e.g., "ns:library"
|
||||
print("Root element: ")
|
||||
print(xml.name(root))
|
||||
|
||||
content = read_file(path)
|
||||
// Extract first book title and author (namespace prefix bk:)
|
||||
b1 = xml.between(content, "<bk:book", "</bk:book>")
|
||||
title = xml.between(b1, "<bk:title>", "</bk:title>")
|
||||
author = xml.between(b1, "<bk:author>", "</bk:author>")
|
||||
print("First book title: ")
|
||||
print(title)
|
||||
print("Author: ")
|
||||
print(author)
|
||||
|
|
@ -37,3 +37,21 @@ class XML()
|
|||
// Get node concatenated text content as string.
|
||||
fun text(this, node)
|
||||
return xml_text(node)
|
||||
|
||||
// Utility: return substring of s between delimiters a and b.
|
||||
// - If a is not found, returns "".
|
||||
// - If b is an empty string, returns everything after the first occurrence of a.
|
||||
// - If b is not found after a, returns "".
|
||||
fun between(this, s, a, b)
|
||||
i = find(s, a)
|
||||
if (i < 0)
|
||||
return ""
|
||||
i = i + len(a)
|
||||
rest = substr(s, i, len(s) - i)
|
||||
if (len(b) == 0)
|
||||
return rest
|
||||
j = find(rest, b)
|
||||
if (j < 0)
|
||||
return ""
|
||||
return substr(rest, 0, j)
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue