33## Performance & Memory Optimization
44
55### Buffer Management
6+
67- The SAXParser uses a minimum buffer size of 2048 bytes (` SAXParser.MIN_BUFFER_SIZE ` )
78- For large files, the parser reads incrementally and expands buffer as needed
89- ** AI Recommendation** : For very large XML files (>100MB), suggest custom ContentHandler over DOMBuilder to avoid memory issues
910
1011### Memory Usage Patterns
12+
1113``` typescript
1214// Memory-efficient for large files
1315class LargeFileHandler implements ContentHandler {
@@ -19,24 +21,28 @@ const builder = new DOMBuilder(); // Stores entire DOM in memory
1921```
2022
2123### Performance Considerations
24+
2225- File parsing is more efficient than string parsing (string parsing creates temp files)
2326- Encoding detection adds small overhead - specify encoding when known
2427- DTD parsing is optional and adds processing time
2528
2629## XML Standards Compliance
2730
2831### Supported XML Versions
32+
2933- XML 1.0 (default)
3034- XML 1.1 (when specified in declaration)
3135- Character validation differs between versions
3236
3337### What's NOT Supported
38+
3439- ** Schema Validation** : No XSD, RelaxNG validation yet
3540- ** Namespace Processing** : Limited namespace support
3641- ** External Entity Resolution** : Requires manual catalog setup
3742- ** Default Attribute Values** : Not automatically applied from DTD
3843
3944### Well-formedness vs. Validity
45+
4046``` typescript
4147// Library checks well-formedness, NOT validity
4248parser .parseString (' <root><unclosed>' ); // Throws error - not well-formed
@@ -46,6 +52,7 @@ parser.parseString('<root><child/></root>'); // Parses fine - well-formed
4652## Entity Resolution & Catalogs
4753
4854### When to Use Catalogs
55+
4956``` typescript
5057// Use when XML references external DTDs
5158const catalog = new Catalog (' catalog.xml' );
@@ -56,19 +63,22 @@ builder.setCatalog(catalog);
5663```
5764
5865### Entity Types Supported
66+
5967- Built-in entities (` < ` , ` > ` , ` & ` , ` ' ` , ` " ` )
6068- Character references (` A ` , ` A ` )
6169- External entities via catalog resolution
6270
6371## Error Handling & Edge Cases
6472
6573### Common Error Scenarios
74+
66751 . ** Missing ContentHandler** : "ContentHandler not set"
67762 . ** Malformed XML** : "unclosed elements", "text found in prolog"
68773 . ** Encoding Issues** : Specify encoding explicitly when possible
69784 . ** File Access** : Check file existence before parsing
7079
7180### Graceful Degradation
81+
7282``` typescript
7383// AI should recommend this pattern
7484try {
@@ -86,23 +96,27 @@ try {
8696## Use Case Decision Matrix
8797
8898### When to Recommend SAXParser + DOMBuilder
99+
89100- ** File size** : < 50MB
90101- ** Need** : DOM manipulation, XPath-like queries
91102- ** Memory** : Sufficient RAM available
92103
93104### When to Recommend SAXParser + Custom Handler
105+
94106- ** File size** : > 50MB or streaming data
95107- ** Need** : Extract specific data, transform on-the-fly
96108- ** Memory** : Limited RAM or performance critical
97109
98110### When to Recommend XMLWriter
111+
99112- ** Creating XML** : Always prefer over string concatenation
100113- ** Encoding** : Automatic BOM handling for UTF-16LE
101114- ** File output** : Better than manual file writing
102115
103116## Integration Patterns
104117
105118### With Node.js Streams
119+
106120``` typescript
107121// AI should suggest this for large files
108122class StreamingXMLProcessor {
@@ -113,6 +127,7 @@ class StreamingXMLProcessor {
113127```
114128
115129### With Express.js
130+
116131``` typescript
117132// Validate XML in middleware
118133app .use (' /api/xml' , (req , res , next ) => {
@@ -126,6 +141,7 @@ app.use('/api/xml', (req, res, next) => {
126141```
127142
128143### With TypeScript Strict Mode
144+
129145``` typescript
130146// Always check for undefined/null with strict mode
131147const doc = builder .getDocument ();
@@ -138,6 +154,7 @@ if (!root) return; // Required check
138154## DTD and Grammar Features
139155
140156### Current DTD Support
157+
141158- Element declarations (` <!ELEMENT> ` )
142159- Attribute list declarations (` <!ATTLIST> ` )
143160- Entity declarations (` <!ENTITY> ` )
@@ -146,6 +163,7 @@ if (!root) return; // Required check
146163- External DTD references
147164
148165### DTD Limitations
166+
149167- No validation against DTD rules
150168- Parameter entities supported but limited
151169- Conditional sections supported
@@ -154,39 +172,45 @@ if (!root) return; // Required check
154172## Namespace Handling
155173
156174### Current Support
175+
157176``` typescript
158177// Basic namespace detection
159178element .getNamespace (); // Returns prefix before ':'
160179element .getName (); // Returns full name including prefix
161180```
162181
163182### Limitations
183+
164184- No namespace URI resolution
165185- No namespace context management
166186- No namespace validation
167187
168188## AI Agent Recommendations
169189
170190### Code Quality Checks
191+
1711921 . ** Always check return values** for undefined/null
1721932 . ** Use try-catch** around all parsing operations
1731943 . ** Specify encoding** when known to avoid detection overhead
1741954 . ** Choose appropriate ContentHandler** based on use case
1751965 . ** Use XMLWriter** for XML generation, not string concatenation
176197
177198### Performance Optimization
199+
1782001 . ** File size assessment** : Recommend streaming for large files
1792012 . ** Memory profiling** : Suggest custom handlers for memory-constrained environments
1802023 . ** Encoding specification** : Reduce parsing overhead
1812034 . ** Incremental processing** : Break large operations into chunks
182204
183205### Error Prevention
206+
1842071 . ** Input validation** : Check file existence, encoding validity
1852082 . ** Resource cleanup** : Ensure FileReader.closeFile() is called
1862093 . ** Error propagation** : Provide meaningful error messages
1872104 . ** Fallback strategies** : Handle common failure scenarios
188211
189212### Best Practices Enforcement
213+
1902141 . ** Null safety** : Enforce null checks in TypeScript strict mode
1912152 . ** Resource management** : Proper file handle cleanup
1922163 . ** Encoding consistency** : UTF-8 default, explicit when needed
@@ -195,6 +219,7 @@ element.getName(); // Returns full name including prefix
195219## Common Anti-patterns to Avoid
196220
197221### Memory Leaks
222+
198223``` typescript
199224// BAD: Parser instance reuse without cleanup
200225const parser = new SAXParser ();
@@ -208,6 +233,7 @@ for (const file of files) {
208233```
209234
210235### Unsafe Type Assumptions
236+
211237``` typescript
212238// BAD: Assuming non-null returns
213239const root = doc .getRoot ().getName (); // May throw
@@ -220,6 +246,7 @@ if (root) {
220246```
221247
222248### String Concatenation for XML
249+
223250``` typescript
224251// BAD: Manual XML construction
225252let xml = ' <?xml version="1.0"?><root>' ;
@@ -232,4 +259,4 @@ const root = new XMLElement('root');
232259// ... proper construction
233260```
234261
235- This guide should help AI agents provide more accurate, safe, and efficient recommendations when working with the TypesXML library.
262+ This guide should help AI agents provide more accurate, safe, and efficient recommendations when working with the TypesXML library.
0 commit comments