question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Filter during parsing?

See original GitHub issue

I would like to be able to parse a subset of an XML document, for example, if I don’t need the entire document, but need a particular deeply-nested tree starting at some path within the XML document.

Is there a way to do this using the new postprocessor feature mentioned in #6? I don’t see a way to exclude elements that way, but it’s possible I am misreading the docs/code.

Issue Analytics

  • State:closed
  • Created 11 years ago
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

2reactions
martinblechcommented, May 22, 2013

I get it. In this case, you should use a postprocessor, not the streaming mode. The postprocessing function gets three arguments in (path, key and value) and must return a (key, value) tuple or None to skip the node altogether.

If you want to skip odd <item>s, you should do something like this:

xml = '''
<doc>
    <item>Item1</item>
    <item>Item2</item>
    <item>Item3</item>
    <item>Item4</item>
</doc>'''

class SkipOddValues:
    def __init__(self, key):
        self.key = key
        self.count = 0

    def __call__(self, path, key, value):
        if key != self.key:
            return key, value
        self.count += 1
        if self.count % 2 == 1:
            return None
        else:
            return key, value

doc = xmltodict.parse(xml, postprocessor=SkipOddValues('item'))
0reactions
martinblechcommented, May 22, 2013

Welcome!

Read more comments on GitHub >

github_iconTop Results From Across the Web

Parser Filters | Block Editor Handbook
To replace the server-side parser, use the block_parser_class filter. The filter transforms the string class name of a parser class. This class is...
Read more >
Filter Before You Parse: Faster Analytics on Raw Data with ...
In this paper, we propose a new approach for reducing this overhead: apply filters on the data's raw bytestream before parsing. This technique,...
Read more >
String Parsing Using a Filter Guide - BMC Communities
First, identify which steps are needed to parse the string and tokenize it. Then, identify which steps will be repeated. The repeatable steps...
Read more >
parser - Fluentd
The parser filter plugin "parses" string field in event records and mutates its event record with the parsed result. It is included in...
Read more >
pig FILTER ERROR 1000: Error during parsing. Encountered
Grunt - ERROR 1000: Error during parsing. Encountered " "filterRowData1=filter "" at line 2, column 1. I have also tried
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found