or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

Files

docs

abdera.mdbadgerfish.mdcli.mdcobra.mdgdata.mdindex.mdparker.mdyahoo.md

parker.mddocs/

0

# Parker Convention

1

2

Parker convention produces the most compact JSON representation by using tail nodes for text content and ignoring XML attributes entirely. It's ideal when attributes aren't needed and minimal JSON output is desired.

3

4

## Capabilities

5

6

### Parker Class

7

8

Creates a Parker converter that ignores attributes and produces compact JSON.

9

10

```python { .api }

11

class Parker(XMLData):

12

def __init__(self, **kwargs):

13

"""

14

Initialize Parker converter.

15

16

Parameters:

17

- **kwargs: Additional XMLData parameters (xml_fromstring, xml_tostring,

18

element, dict_type, list_type, simple_text, invalid_tags)

19

"""

20

```

21

22

### Data to XML Conversion

23

24

Converts Python dictionaries and lists to XML elements using Parker convention.

25

26

```python { .api }

27

def etree(self, data, root=None):

28

"""

29

Convert data structure into a list of etree.Element.

30

31

Parameters:

32

- data: dict or list, data structure to convert

33

- root: Element, optional root element to append to

34

35

Returns:

36

List of etree.Element objects or modified root element

37

"""

38

```

39

40

### XML to Data Conversion

41

42

Converts XML elements to Python dictionaries using Parker convention with optional root preservation.

43

44

```python { .api }

45

def data(self, root, preserve_root=False):

46

"""

47

Convert etree.Element into a dictionary.

48

49

Parameters:

50

- root: Element, XML element to convert

51

- preserve_root: bool, whether to preserve root element (default: False)

52

53

Returns:

54

dict or primitive: Dictionary representation using Parker convention

55

"""

56

```

57

58

**Usage Examples:**

59

60

```python

61

from xmljson import parker

62

from xml.etree.ElementTree import fromstring

63

import json

64

65

# Basic conversion (root absorbed)

66

xml_string = '<x><a>1</a><b>2</b></x>'

67

xml_element = fromstring(xml_string)

68

data = parker.data(xml_element)

69

print(json.dumps(data))

70

# Output: {"a": 1, "b": 2}

71

72

# Preserve root element

73

data = parker.data(xml_element, preserve_root=True)

74

print(json.dumps(data))

75

# Output: {"x": {"a": 1, "b": 2}}

76

77

# Simple text elements

78

xml_string = '<item>Hello World</item>'

79

xml_element = fromstring(xml_string)

80

data = parker.data(xml_element)

81

print(data)

82

# Output: "Hello World"

83

```

84

85

### Pre-configured Instance

86

87

```python { .api }

88

parker: Parker

89

```

90

91

A pre-configured Parker instance available at module level for immediate use.

92

93

## Convention Rules

94

95

- **Attributes**: Completely ignored and not included in output

96

- **Root Element**: Absorbed by default (can be preserved with `preserve_root=True`)

97

- **Text Content**: Becomes the direct value (no special key)

98

- **Empty Elements**: Become empty strings

99

- **Arrays**: Multiple elements with same name become arrays

100

- **Type Conversion**: Automatic conversion of strings to booleans, integers, and floats

101

- **Whitespace**: Text content is automatically trimmed

102

103

## Use Cases

104

105

- **Configuration Files**: When XML attributes aren't needed

106

- **Data Exchange**: Minimal JSON payload size required

107

- **Simple XML Structures**: Converting basic XML data to JSON

108

- **API Responses**: When compact JSON representation is preferred

109

110

## Limitations

111

112

- **No Attribute Support**: XML attributes are completely lost

113

- **No Namespace Support**: XML namespaces are not handled

114

- **Root Information**: Root element name lost unless preserve_root=True