Skip to content

Commit 0f7abed

Browse files
authored
Add suggestion about omitting comments (#5)
Closes #4.
1 parent 9d78886 commit 0f7abed

File tree

1 file changed

+6
-4
lines changed

1 file changed

+6
-4
lines changed

README.md

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ class Container(XmlDataclass):
147147
if __name__ == "__main__":
148148
nsmap: NsMap = {None: CONTAINER_NS}
149149
# see Gotchas, stripping whitespace is highly recommended
150-
parser = etree.XMLParser(remove_blank_text=True)
150+
parser = etree.XMLParser(remove_blank_text=True, remove_comments=True)
151151
lxml_el_in = etree.parse("container.xml", parser).getroot()
152152
container = load(Container, lxml_el_in, "container")
153153
lxml_el_out = dump(container, "container", nsmap)
@@ -176,16 +176,18 @@ class Parent(XmlDataclass):
176176

177177
It's important that `@dataclass` be the *last* decorator, i.e. the closest to the class definition (and so the first to be applied). Luckily, only the root class you intend to pass to `load`/`dump` has to inherit from `XmlDataclass`, but all classes should have the `@dataclass` decorator applied.
178178

179-
### Whitespace
179+
### Whitespace and comments
180180

181-
If you are able to, it is strongly recommended you strip whitespace from the input via `lxml`:
181+
If you are able to, it is strongly recommended you strip whitespace and comments from the input via `lxml`:
182182

183183
```python
184-
parser = etree.XMLParser(remove_blank_text=True)
184+
parser = etree.XMLParser(remove_blank_text=True, remove_comments=True)
185185
```
186186

187187
By default, `lxml` preserves whitespace. This can cause a problem when checking if elements have no text. The library does attempt to strip these; literally via Python's `strip()`. But `lxml` is likely faster and more robust.
188188

189+
Similarly, comments are included by default, and because deserialization is strict, they will be considered as nodes that the dataclass has not declared. It is recommended to omit them during parsing.
190+
189191
### Optional vs required
190192

191193
On dataclasses, optional fields also usually have a default value to be useful. But this isn't required; `Optional` is just a type hint to say `None` is allowed. This would occur e.g. if an element has no children.

0 commit comments

Comments
 (0)