xmlproxy
Wrapper to provide dict-like access to XML via ElementTree.
XMLProxy ¶
Class providing dict-like access to edit XML via ElementTree.
Note that this wrapper facade is limited to a restricted (but useful) subset of XML: * XML attributes are not supported * DTDs are ignored (arbitrary keys can be queried and added) * each tag is assumed to EITHER contain text OR more nested tags * lists are treated atomically (no way to add/remove element from a collection)
The semantics is implemented as follows:
- If there are multiple tags with the same name, a list of XMLProxy nodes is returned
- If a unique tag does have no nested tags, its
text
string value is returned - Otherwise, the node is returned
Source code in src/somesy/pom_xml/xmlproxy.py
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 |
|
tag
property
writable
¶
tag: Optional[str]
Return tag name of this element (unless it is a comment).
__init__ ¶
__init__(
el: ET.Element,
*,
default_namespace: Optional[str] = None
)
Wrap an existing XML ElementTree Element.
Source code in src/somesy/pom_xml/xmlproxy.py
76 77 78 79 |
|
parse
classmethod
¶
parse(path: Union[str, Path], **kwargs) -> XMLProxy
Parse an XML file into a wrapped ElementTree, preserving comments.
Source code in src/somesy/pom_xml/xmlproxy.py
81 82 83 84 85 |
|
write ¶
write(
path: Union[str, Path], *, header: bool = True, **kwargs
)
Write the XML DOM to an UTF-8 encoded file.
Source code in src/somesy/pom_xml/xmlproxy.py
87 88 89 90 91 92 93 94 |
|
__repr__ ¶
__repr__()
See object.__repr__
.
Source code in src/somesy/pom_xml/xmlproxy.py
96 97 98 |
|
__len__ ¶
__len__()
Return number of inner tags inside current XML element.
Note that bool(node) thus checks whether an XML node is a leaf in the element tree.
Source code in src/somesy/pom_xml/xmlproxy.py
100 101 102 103 104 105 |
|
__iter__ ¶
__iter__()
Iterate the nested elements in-order.
Source code in src/somesy/pom_xml/xmlproxy.py
107 108 109 |
|
to_jsonlike ¶
to_jsonlike(
*,
strip_default_ns: bool = True,
keep_root: bool = False
) -> JSONLike
Convert XML node to a JSON-like primitive, array or dict (ignoring attributes).
Note that all leaf values are strings (i.e. not parsed to bool/int/float etc.).
Parameters:
Name | Type | Description | Default |
---|---|---|---|
strip_default_ns |
bool
|
Do not qualify keys from the default namespace |
True
|
keep_root |
bool
|
If true, the root tag name will be preserved ( |
False
|
Source code in src/somesy/pom_xml/xmlproxy.py
137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 |
|
from_jsonlike
classmethod
¶
from_jsonlike(
val: JSONLike,
*,
root_name: Optional[str] = None,
**kwargs: Any
)
Convert a JSON-like primitive, array or dict into an XML element.
Note that booleans are serialized as true
/false
and None as null
.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
val |
JSONLike
|
Value to convert into an XML element. |
required |
root_name |
Optional[str]
|
If |
None
|
kwargs |
Any
|
Additional arguments for XML element instantiation. |
{}
|
Source code in src/somesy/pom_xml/xmlproxy.py
203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 |
|
get ¶
get(
key: str, *, as_nodes: bool = False, deep: bool = False
)
Get sub-structure(s) of value(s) matching desired XML tag name.
- If there are multiple matching elements, will return them all as a list.
- If there is a single matching element, will return that element without a list.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
key |
str
|
tag name to retrieve |
required |
as_nodes |
bool
|
If true, will always return a list of (zero or more) XML nodes |
False
|
deep |
bool
|
Expand nested XML elements instead of returning them as XML nodes |
False
|
Source code in src/somesy/pom_xml/xmlproxy.py
248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 |
|
__getitem__ ¶
__getitem__(key: str)
Acts like dict.__getitem__
, implemented with get
.
Source code in src/somesy/pom_xml/xmlproxy.py
281 282 283 284 285 286 287 |
|
__contains__ ¶
__contains__(key: str) -> bool
Acts like dict.__contains__
, implemented with get
.
Source code in src/somesy/pom_xml/xmlproxy.py
289 290 291 |
|
__delitem__ ¶
__delitem__(key: Union[str, XMLProxy])
Delete a nested XML element with matching key name.
Note that all XML elements with the given tag name are removed!
To prevent this behavior, instead of a string tag name you can provide the
exact element to be removed, i.e. if a node node_a
represents the following XML:
<a>
<b>1</b>
<c>2</c>
<b>3</b>
</a>
Then we have that:
del node_a["b"]
removes both tags, leaving just thec
tag.del node_a[node_a["a"][1]]
removes just the second tag with the3
.
Source code in src/somesy/pom_xml/xmlproxy.py
293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 |
|
__setitem__ ¶
__setitem__(
key: Union[str, XMLProxy],
val: Union[JSONLike, XMLProxy],
)
Add or overwrite an inner XML tag.
If there is exactly one matching tag, the value is substituted in-place. If the passed value is a list, all list entries are added in their own element.
If there are multiple existing matches or target values, then all existing elements are removed and the new value(s) are added in new element(s) (i.e. coming after other unrelated existing elements)!
To prevent this behavior, instead of a string tag name you can provide the
exact element to be overwritten, i.e. if a node node_a
represents the following XML:
<a>
<b>1</b>
<c>2</c>
<b>3</b>
</a>
Then we have that:
node_a["b"] = 5
removes both existing tags and creates a new tag with the passed value(s).node_a[node_a["b"][1]] = 5
replaces the3
in the second tag with the5
.
Note that the passed value must be either an XML element already, or be a pure JSON-like object.
Source code in src/somesy/pom_xml/xmlproxy.py
334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 |
|
load_xml ¶
load_xml(path: Path) -> ET.ElementTree
Parse an XML file into an ElementTree, preserving comments.
Source code in src/somesy/pom_xml/xmlproxy.py
14 15 16 17 18 |
|
indent ¶
indent(elem, level=0)
Indent the elements of this XML node (i.e. pretty print).
Source code in src/somesy/pom_xml/xmlproxy.py
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
|