开发者

Is it possible to use Apache Digester to filter dynamic xml leaf tags?

开发者 https://www.devze.com 2023-03-05 18:25 出处:网络
I\'ve used Apache digester before and loved the branch based searching of xml tags. Specifying a tag as

I've used Apache digester before and loved the branch based searching of xml tags. Specifying a tag as h\a\b\ is very intuitive.

Now i want to do xml filtering project, but apache digester doesn't seem like it will work, simply because there is no way to get to the underlying xml tags. As the faq says:

How do I get some xml nested within a tag as a literal string?

It is frequently asked how some XML (esp. XHTML) nested within a document can be extracted as a string, eg to extract the contents of the "body" tag below as a string:

...some xml code...

If you can modify the above to wrap the desired text as a CDATA section then things are easy; Digester will simply treat that CDATA block as a single string:

...some xml code...

If this can't be done then you need to use a NodeCreateRule to create a DOM node representing the body tag and its children, then serialise that DOM node back to text.

Remember that Digester is just a layer on top of a standard XML pars开发者_开发问答er, and standard XML parsers have no option to just stop parsing input at a specific element - unless it knows that the contents of that element is a block of characters (CDATA).

If there was something that uses the same pattern system that i can use to filter xml? My idea is to use the patterns given by the user and blacklist them, and copy everything else.

Or maybe there is a way to find the location of a match in Apache Digester (the location on the xml, not just the displayed text). That would be enough for me to copy the other text by keeping a copy of it around, and skipping the matches.

Edit: I've since found out that XPath looks almost ok for doing this, but all applications i found were for selecting something, not removing it. Do you have a example for this?


Never mind, managed to do it with XPath.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号