gsnedders: fixes #228 
landscape-bot: [![Code Health](https://landscape.io/badge/381520/landscape.svg?style=flat)](https://landscape.io/diff/354583)
Code quality remained the same when pulling **[2f1d040](https://github.com/gsnedders/html5lib-python/commit/2f1d04073f63e7633b8fa2204e7dac221051c7fe) on gsnedders:lxml_treewalker_lxml_tree** into **[5288737](https://github.com/html5lib/html5lib-python/commit/5288737aebcae1fcf25a640c79241f6fb14475a2) on html5lib:master**.
codecov-io: ## [Current coverage][cc-pull] is **90.83%**
> Merging [#256][cc-pull] into [master][cc-base-branch] will increase coverage by **<.01%**

```diff
@@             master       #256   diff @@
==========================================
  Files            51         51          
  Lines          6821       6835    +14   
  Methods           0          0          
  Messages          0          0          
  Branches       1313       1312     -1   
==========================================
+ Hits           6193       6208    +15   
  Misses          468        468          
+ Partials        160        159     -1   
```

> Powered by [Codecov](https://codecov.io?src=pr). Last updated by [5288737...2f1d040][cc-compare]
[cc-base-branch]: https://codecov.io/gh/html5lib/html5lib-python/branch/master?src=pr
[cc-compare]: https://codecov.io/gh/html5lib/html5lib-python/compare/5288737aebcae1fcf25a640c79241f6fb14475a2...2f1d04073f63e7633b8fa2204e7dac221051c7fe
[cc-pull]: https://codecov.io/gh/html5lib/html5lib-python/pull/256?src=pr
landscape-bot: [![Code Health](https://landscape.io/badge/385132/landscape.svg?style=flat)](https://landscape.io/diff/358204)
Code quality remained the same when pulling **[a2ee6e6](https://github.com/gsnedders/html5lib-python/commit/a2ee6e6b0ec015a3a777546192d8af80074d60b0) on gsnedders:lxml_treewalker_lxml_tree** into **[66a2f77](https://github.com/html5lib/html5lib-python/commit/66a2f7763f2dda24d3d2681c22bf799c94ee049c) on html5lib:master**.
diff --git a/html5lib/treewalkers/lxmletree.py b/html5lib/treewalkers/lxmletree.py
index 7d99adc..ff31a44 100644
--- a/html5lib/treewalkers/lxmletree.py
+++ b/html5lib/treewalkers/lxmletree.py
@@ -22,13 +22,20 @@ class Root(object):
     def __init__(self, et):
         self.elementtree = et
         self.children = []
-        if et.docinfo.internalDTD:
-            self.children.append(Doctype(self,
-                                         ensure_str(et.docinfo.root_name),
-                                         ensure_str(et.docinfo.public_id),
-                                         ensure_str(et.docinfo.system_url)))
-        root = et.getroot()
-        node = root
+
+        try:
+            if et.docinfo.internalDTD:
+                self.children.append(Doctype(self,
+                                             ensure_str(et.docinfo.root_name),
+                                             ensure_str(et.docinfo.public_id),
+                                             ensure_str(et.docinfo.system_url)))
+        except AttributeError:
+            pass
+
+        try:
+            node = et.getroot()
+        except AttributeError:
+            node = et
 
         while node.getprevious() is not None:
             node = node.getprevious()
@@ -118,12 +125,12 @@ class FragmentWrapper(object):
 class TreeWalker(_base.NonRecursiveTreeWalker):
     def __init__(self, tree):
         # pylint:disable=redefined-variable-type
-        if hasattr(tree, "getroot"):
-            self.fragmentChildren = set()
-            tree = Root(tree)
-        elif isinstance(tree, list):
+        if isinstance(tree, list):
             self.fragmentChildren = set(tree)
             tree = FragmentRoot(tree)
+        else:
+            self.fragmentChildren = set()
+            tree = Root(tree)
         _base.NonRecursiveTreeWalker.__init__(self, tree)
         self.filter = ihatexml.InfosetFilter()
 

AttributeError: 'TreeWalker' object has no attribute 'fragmentChildren'
For some reason my `tree` (`lxml.etree._Element`) object has no attribute getroot and is not a list so there is no `fragmentChildren` attribute on `TreeWalker`: 270a2ca14fafc989f8f1bd4f79db2f4bd9f4d1fc

lxml 3.5.0
html5lib-python f796cca5f9ddaaf1e1a8b872f68455551cd3ae2d
diff --git a/html5lib/tests/test_treewalkers.py b/html5lib/tests/test_treewalkers.py
index 332027a..81ed277 100644
--- a/html5lib/tests/test_treewalkers.py
+++ b/html5lib/tests/test_treewalkers.py
@@ -2,6 +2,11 @@ from __future__ import absolute_import, division, unicode_literals
 
 import pytest
 
+try:
+    import lxml.etree
+except ImportError:
+    pass
+
 from .support import treeTypes
 
 from html5lib import html5parser, treewalkers
@@ -93,3 +98,19 @@ def test_treewalker_six_mix():
     for tree in sorted(treeTypes.items()):
         for intext, attrs, expected in sm_tests:
             yield runTreewalkerEditTest, intext, expected, attrs, tree
+
+
+@pytest.mark.skipif(treeTypes["lxml"] is None, reason="lxml not importable")
+def test_lxml_xml():
+    expected = [
+        {'data': {}, 'name': 'div', 'namespace': None, 'type': 'StartTag'},
+        {'data': {}, 'name': 'div', 'namespace': None, 'type': 'StartTag'},
+        {'name': 'div', 'namespace': None, 'type': 'EndTag'},
+        {'name': 'div', 'namespace': None, 'type': 'EndTag'}
+    ]
+
+    lxmltree = lxml.etree.fromstring('<div><div></div></div>')
+    walker = treewalkers.getTreeWalker('lxml')
+    output = Lint(walker(lxmltree))
+
+    assert list(output) == expected

pip install -e .[all]
parse_log_pytest
requirements.txt
pytest mock
git submodule update --init --recursive
3.9
requirements.txt
pytest --no-header -rA --tb=line --color=no -p no:cacheprovider -W ignore::DeprecationWarning
chardet==5.2.0
datrie==0.8.2
exceptiongroup==1.2.2
Genshi==0.7.9
-e git+https://github.com/html5lib/html5lib-python.git@66a2f7763f2dda24d3d2681c22bf799c94ee049c#egg=html5lib
iniconfig==2.1.0
lxml==5.3.1
mock==5.2.0
packaging==24.2
pluggy==1.5.0
pytest==8.3.5
six==1.17.0
tomli==2.2.1
webencodings==0.5.1

name: html5lib-python
channels:
  - defaults
  - https://repo.anaconda.com/pkgs/main
  - https://repo.anaconda.com/pkgs/r
  - conda-forge
dependencies:
  - _libgcc_mutex=0.1=main
  - _openmp_mutex=5.1=1_gnu
  - ca-certificates=2025.2.25=h06a4308_0
  - ld_impl_linux-64=2.40=h12ee557_0
  - libffi=3.4.4=h6a678d5_1
  - libgcc-ng=11.2.0=h1234567_1
  - libgomp=11.2.0=h1234567_1
  - libstdcxx-ng=11.2.0=h1234567_1
  - ncurses=6.4=h6a678d5_0
  - openssl=3.0.16=h5eee18b_0
  - pip=25.0=py39h06a4308_0
  - python=3.9.21=he870216_1
  - readline=8.2=h5eee18b_0
  - setuptools=75.8.0=py39h06a4308_0
  - sqlite=3.45.3=h5eee18b_0
  - tk=8.6.14=h39e8969_0
  - tzdata=2025a=h04d1e81_0
  - wheel=0.45.1=py39h06a4308_0
  - xz=5.6.4=h5eee18b_1
  - zlib=1.2.13=h5eee18b_1
  - pip:
      - chardet==5.2.0
      - datrie==0.8.2
      - exceptiongroup==1.2.2
      - genshi==0.7.9
      - iniconfig==2.1.0
      - lxml==5.3.1
      - mock==5.2.0
      - packaging==24.2
      - pluggy==1.5.0
      - pytest==8.3.5
      - six==1.17.0
      - tomli==2.2.1
      - webencodings==0.5.1
prefix: /opt/conda/envs/html5lib-python

html5lib/tests/test_treewalkers.py::test_lxml_xml
html5lib/tests/test_treewalkers.py::test_all_tokens
html5lib/treewalkers/lxmletree.py
html5lib/treewalkers/lxmletree.py