WriteWithOptions writes xml with given options to given writer.
(writer io.Writer, opts ...OutputOption)
| 367 | |
| 368 | // WriteWithOptions writes xml with given options to given writer. |
| 369 | func (n *Node) WriteWithOptions(writer io.Writer, opts ...OutputOption) (err error) { |
| 370 | config := &outputConfiguration{ |
| 371 | preserveSpaces: true, |
| 372 | } |
| 373 | // Set the options |
| 374 | for _, opt := range opts { |
| 375 | opt(config) |
| 376 | } |
| 377 | pastPreserveSpaces := config.preserveSpaces |
| 378 | preserveSpaces := calculatePreserveSpaces(n, pastPreserveSpaces) |
| 379 | b := bufio.NewWriter(writer) |
| 380 | defer b.Flush() |
| 381 | |
| 382 | ident := newIndentation(config.useIndentation, b) |
| 383 | if config.printSelf && n.Type != DocumentNode { |
| 384 | err = outputXML(b, n, preserveSpaces, config, ident) |
| 385 | } else { |
| 386 | for n := n.FirstChild; n != nil; n = n.NextSibling { |
| 387 | err = outputXML(b, n, preserveSpaces, config, ident) |
| 388 | if err != nil { |
| 389 | break |
| 390 | } |
| 391 | } |
| 392 | } |
| 393 | return |
| 394 | } |
| 395 | |
| 396 | // AddAttr adds a new attribute specified by 'key' and 'val' to a node 'n'. |
| 397 | // Returns false if the attribute already exists. |
no test coverage detected