来自xml的JSoup Strip html标记(JSoup Strip html markup from xml)
我一直在寻找stackoverflow但无法让任何人遇到这种问题。
我想做这样的事情:
输入字符串:
<?xml version="1.0" encoding="UTF-8" ?> <List> <Object> <Section>Fruit</Section> <Category>Bananas</Category> <Brand>Chiquita</Brand> <Obs><p> Vende-se a peças ou o conjunto.</p><br> </Obs> </Object> </List>我想要的是剥离html标签,如<p>,<br>等。所以它结束如下:
<?xml version="1.0" encoding="UTF-8" ?> <List> <Object> <Section>Fruit</Section> <Category>Bananas</Category> <Brand>Chiquita</Brand> <Obs> Vende-se a peças ou o conjunto. </Obs> </Object> </List>我一直在玩JSoup,但我似乎无法让它正常工作。
这是我的代码:
Whitelist whitelist = Whitelist.none(); String xml = "<?xml version=\"1.0\" encoding=\"UTF-8\" ?><List><Object><Section>Fruit</Section><Category>Bananas</Category><Brand>Chiquita</Brand><Obs><p>Vende-se a peças ou o conjunto.</p><br></Obs></Object></List>"; whitelist.addTags(new String[]{"?xml", "List", "Object", "Section", "Category", "Brand", "Obs"}); String safe = Jsoup.clean(xml, whitelist);这是我获得的结果:
FruitBananasChiquitaVende-se a peças ou o conjunto.提前致谢
i've been looking stackoverflow but couldn't get anyone with this kind of problem.
I want to do something like this:
Input String:
<?xml version="1.0" encoding="UTF-8" ?> <List> <Object> <Section>Fruit</Section> <Category>Bananas</Category> <Brand>Chiquita</Brand> <Obs><p> Vende-se a peças ou o conjunto.</p><br> </Obs> </Object> </List>What i want is to strip html tags, like <p>,<br> etc. So it ends like this:
<?xml version="1.0" encoding="UTF-8" ?> <List> <Object> <Section>Fruit</Section> <Category>Bananas</Category> <Brand>Chiquita</Brand> <Obs> Vende-se a peças ou o conjunto. </Obs> </Object> </List>I have been playing around with JSoup, but i can't seem to make it work properly.
This is the code i have:
Whitelist whitelist = Whitelist.none(); String xml = "<?xml version=\"1.0\" encoding=\"UTF-8\" ?><List><Object><Section>Fruit</Section><Category>Bananas</Category><Brand>Chiquita</Brand><Obs><p>Vende-se a peças ou o conjunto.</p><br></Obs></Object></List>"; whitelist.addTags(new String[]{"?xml", "List", "Object", "Section", "Category", "Brand", "Obs"}); String safe = Jsoup.clean(xml, whitelist);This is the result i am obtaining:
FruitBananasChiquitaVende-se a peças ou o conjunto.Thanks in advance
最满意答案
标签是小写的,使用:
whitelist.addTags(new String[] { "?xml", "list", "object", "section", "category", "brand", "obs" });输出:
<list> <object> <section> Fruit </section> <category> Bananas </category> <brand> Chiquita </brand> <obs> Vende-se a peças ou o conjunto. </obs></object> </list>tags are lowercased, use:
whitelist.addTags(new String[] { "?xml", "list", "object", "section", "category", "brand", "obs" });output:
<list> <object> <section> Fruit </section> <category> Bananas </category> <brand> Chiquita </brand> <obs> Vende-se a peças ou o conjunto. </obs></object> </list>更多推荐
发布评论