Skip to content

untanglevscascadia

MIT 21 2 632
442.1 thousand (month) Jun 09 2011 1.2.1(2022-07-02 14:09:28 ago)
754 1 1 BSD-2-Clause
Feb 20 2018 58.1 thousand (month) Start(2018-02-20 18:47:44 ago)

untangle is a simple library for parsing XML documents in Python. It allows you to access data in an XML file as if it were a Python object, making it easy to work with the data in your code.

To use untangle, you first need to install it via pip by running pip install untangle``. Once it is installed, you can use theuntangle.parse()`` function to parse an XML file and create a Python object.

For example: ``` import untangle

obj = untangle.parse("example.xml") print(obj.root.element.child) ```

You can also pass a file-like object or a string containing XML data to the untangle.parse() function. Once you have an untangle object, you can access elements in the XML document using dot notation.

You can also access the attributes of an element by using attrib property, eg. `obj.root.element['attrib_name']`` untangle also supports xpath-like syntax to access the elements, obj.root.xpath("path/to/element")

It also supports iteration over the elements using obj.root.element.children python for child in obj.root.element.children: print(child)

cascadia is a library for Go that provides a CSS selector engine, allowing you to use CSS selectors to select elements from an HTML document.

It is built on top of the html package in the Go standard library, and provides a more efficient and powerful way to select elements from an HTML document.

Example Use


```python import untangle obj = untangle.parse("example.xml") print(obj.root.element.child) # access attributes: print(obj.root.element['attrib_name']) # use xpath: element = obj.root.xpath("path/to/element") ```
```go package main import ( "fmt" "github.com/andybalholm/cascadia" "golang.org/x/net/html" "strings" ) func main() { // Create an HTML string html := `

Hello, World!

Example
` // Parse the HTML string into a node tree doc, err := html.Parse(strings.NewReader(html)) if err != nil { fmt.Println("Error:", err) return } // Compile the CSS selector sel, err := cascadia.Compile("p") if err != nil { fmt.Println("Error:", err) return } // Use the Selector.Match method to select elements from the document matches := sel.Match(doc) if len(matches) > 0 { fmt.Println(matches[0].FirstChild.Data) // > Hello, World! } } ```

Alternatives / Similar


Was this page helpful?