or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

Files

docs

build-config.mdcore-model.mddependencies.mdindex.mdmodel-merging.mdprofiles.mdproject-metadata.mdrepositories.mdutility-classes.mdxml-io.md

xml-io.mddocs/

0

# XML I/O Operations

1

2

XML readers and writers for serializing models to and from POM files with location tracking and formatting preservation. These classes handle the conversion between Maven Model objects and XML POM files.

3

4

## Capabilities

5

6

### MavenXpp3Reader

7

8

Standard XML reader for parsing POM files into Model objects using XPP3 parser.

9

10

```java { .api }

11

public class MavenXpp3Reader {

12

// Basic reading methods

13

public Model read(Reader reader) throws IOException, XmlPullParserException;

14

public Model read(InputStream in) throws IOException, XmlPullParserException;

15

public Model read(Reader reader, boolean strict) throws IOException, XmlPullParserException;

16

public Model read(InputStream in, boolean strict) throws IOException, XmlPullParserException;

17

18

// Advanced methods with encoding

19

public Model read(Reader reader, boolean strict, InputSource source) throws IOException, XmlPullParserException;

20

public Model read(InputStream in, boolean strict, String encoding, InputSource source) throws IOException, XmlPullParserException;

21

}

22

```

23

24

**Usage Example:**

25

26

```java

27

import org.apache.maven.model.Model;

28

import org.apache.maven.model.io.xpp3.MavenXpp3Reader;

29

import java.io.FileReader;

30

import java.io.IOException;

31

import org.codehaus.plexus.util.xml.pull.XmlPullParserException;

32

33

MavenXpp3Reader reader = new MavenXpp3Reader();

34

35

try {

36

// Read from file

37

Model model = reader.read(new FileReader("pom.xml"));

38

39

// Read with strict parsing disabled (allows unknown elements)

40

Model model2 = reader.read(new FileReader("pom.xml"), false);

41

42

System.out.println("Project: " + model.getArtifactId());

43

System.out.println("Version: " + model.getVersion());

44

45

} catch (IOException | XmlPullParserException e) {

46

System.err.println("Error reading POM: " + e.getMessage());

47

}

48

```

49

50

### MavenXpp3ReaderEx

51

52

Extended XML reader with location tracking support for error reporting and IDE integration.

53

54

```java { .api }

55

public class MavenXpp3ReaderEx {

56

// Reading methods with location tracking

57

public Model read(Reader reader) throws IOException, XmlPullParserException;

58

public Model read(InputStream in) throws IOException, XmlPullParserException;

59

public Model read(Reader reader, boolean strict) throws IOException, XmlPullParserException;

60

public Model read(InputStream in, boolean strict) throws IOException, XmlPullParserException;

61

62

// Advanced methods with encoding and source tracking

63

public Model read(Reader reader, boolean strict, InputSource source) throws IOException, XmlPullParserException;

64

public Model read(InputStream in, boolean strict, String encoding, InputSource source) throws IOException, XmlPullParserException;

65

}

66

```

67

68

**Usage Example:**

69

70

```java

71

import org.apache.maven.model.Model;

72

import org.apache.maven.model.InputLocation;

73

import org.apache.maven.model.io.xpp3.MavenXpp3ReaderEx;

74

75

MavenXpp3ReaderEx readerEx = new MavenXpp3ReaderEx();

76

77

try {

78

Model model = readerEx.read(new FileReader("pom.xml"), true);

79

80

// Access location information for error reporting

81

InputLocation location = model.getLocation("version");

82

if (location != null) {

83

System.out.println("Version defined at line: " + location.getLineNumber());

84

}

85

86

} catch (Exception e) {

87

System.err.println("Parse error: " + e.getMessage());

88

}

89

```

90

91

### MavenXpp3Writer

92

93

Standard XML writer for serializing Model objects to POM XML format.

94

95

```java { .api }

96

public class MavenXpp3Writer {

97

// Basic writing methods

98

public void write(Writer writer, Model model) throws IOException;

99

public void write(OutputStream stream, Model model) throws IOException;

100

101

// Methods with encoding control

102

public void write(Writer writer, Model model, String encoding) throws IOException;

103

public void write(OutputStream stream, Model model, String encoding) throws IOException;

104

}

105

```

106

107

**Usage Example:**

108

109

```java

110

import org.apache.maven.model.Model;

111

import org.apache.maven.model.io.xpp3.MavenXpp3Writer;

112

import java.io.FileWriter;

113

import java.io.IOException;

114

115

// Create or modify model

116

Model model = new Model();

117

model.setModelVersion("4.0.0");

118

model.setGroupId("com.example");

119

model.setArtifactId("my-project");

120

model.setVersion("1.0.0");

121

122

MavenXpp3Writer writer = new MavenXpp3Writer();

123

124

try {

125

// Write to file

126

writer.write(new FileWriter("output-pom.xml"), model);

127

128

// Write to file with specific encoding

129

writer.write(new FileWriter("utf8-pom.xml"), model, "UTF-8");

130

131

System.out.println("POM file written successfully");

132

133

} catch (IOException e) {

134

System.err.println("Error writing POM: " + e.getMessage());

135

}

136

```

137

138

### MavenXpp3WriterEx

139

140

Extended XML writer with advanced formatting options and comment preservation.

141

142

```java { .api }

143

public class MavenXpp3WriterEx {

144

// Writing methods with extended formatting

145

public void write(Writer writer, Model model) throws IOException;

146

public void write(OutputStream stream, Model model) throws IOException;

147

148

// Methods with encoding and formatting control

149

public void write(Writer writer, Model model, String encoding) throws IOException;

150

public void write(OutputStream stream, Model model, String encoding) throws IOException;

151

}

152

```

153

154

**Key Features:**

155

156

- **Location Tracking**: Extended reader tracks source locations for each model element

157

- **Error Reporting**: Precise line/column information for validation errors

158

- **Comment Preservation**: Extended writer can preserve XML comments and formatting

159

- **Encoding Support**: Full UTF-8 and other encoding support

160

- **Strict vs Lenient**: Option to ignore unknown XML elements for forward compatibility

161

162

### Error Handling

163

164

All I/O operations can throw these exceptions:

165

166

```java

167

// XML parsing errors

168

catch (XmlPullParserException e) {

169

System.err.println("XML syntax error: " + e.getMessage());

170

System.err.println("Line: " + e.getLineNumber() + ", Column: " + e.getColumnNumber());

171

}

172

173

// File I/O errors

174

catch (IOException e) {

175

System.err.println("File I/O error: " + e.getMessage());

176

}

177

```

178

179

### InputSource and InputLocation

180

181

Used by extended readers for tracking source information:

182

183

```java { .api }

184

public class InputSource {

185

public String getLocation();

186

public String getModelId();

187

}

188

189

public class InputLocation {

190

public int getLineNumber();

191

public int getColumnNumber();

192

public InputSource getSource();

193

public Map<Object, InputLocation> getLocations();

194

}

195

```

196

197

**Usage for IDE Integration:**

198

199

```java

200

// Create input source for tracking

201

InputSource source = new InputSource();

202

source.setLocation("pom.xml");

203

source.setModelId("com.example:my-project:1.0.0");

204

205

// Read with source tracking

206

Model model = readerEx.read(new FileReader("pom.xml"), true, source);

207

208

// Later, get location of any element for error reporting or IDE navigation

209

InputLocation versionLoc = model.getLocation("version");

210

if (versionLoc != null) {

211

int line = versionLoc.getLineNumber();

212

int column = versionLoc.getColumnNumber();

213

String file = versionLoc.getSource().getLocation();

214

// Use for IDE "Go to Definition" or error highlighting

215

}

216

```