I have an object A which contains an object B. B has some fields. Is is possible, when serializing A to XML with XStream, to have all fields of B appear at the same level as fields of A?
More concretely, I have the following classes:
class B{
String foo = "bar";
}
class A{
B b;
}
I would like the output to look like:
<A>
<foo>bar</foo>
</A>
Instead of
<A>
<B>
<foo>bar</foo>
</B>
</A>
Related
<div class="outer">
<div class= ""></div>
<div class= "inner">
text1
text2
text3
</div>
</div>
Lets say there is an outer div which holds couple of child divs. First one has no class name second one includes anchor tags. And the page has a lot of divs class named "outer". How can I get texts inside these a tags? And I want to count the number of anchor tags' texts inside div class="inner". Because page has a lot of divs with class named="outer" and these divs holds different number of a href tags inside child div class named="inner".
to get a inside div.outer > div.inner do loop
outers = soup.select('div.outer')
for inner in outers:
atags = inner.select('div.inner a')
print(len(atags))
for a in atags:
print(a['href'])
I have the following:
<div id="header-author" class="some random class">
<a id="author-text" class="some random class" href="/page?id=232">
<span class="some random class">
Hello there
</span>
</a>
and i want to extract only href attributes of id="author-text"
i cant use class to extract because the class is used by other elements which has href links which i do not want to extract
i have tried this
soupeddata = BeautifulSoup(my_html_code, "html.parser")
my_data = soupeddata.find_all("a", id= "author-text")
for x in my_data:
my_href = x.get("href")
print(my_href)
Thank you in advance and will be sure to upvote/accept answer!
Use this:
my_data = soupeddata.find_all('a', attrs = {'id': 'author-text'})
You can also pass class attribute inside the dict.
From the BeautifulSoup documentation:
Some attributes, like the data-* attributes in HTML 5, have names that
can’t be used as the names of keyword arguments:
data_soup = BeautifulSoup('<div data-foo="value">foo!</div>')
data_soup.find_all(data-foo="value")
# SyntaxError: keyword can't be an expression
You can use these attributes in searches by putting them
into a dictionary and passing the dictionary into find_all() as
the attrs argument:
data_soup.find_all(attrs={"data-foo": "value"})
# [<div data-foo="value">foo!</div>]
I need to traverse through a XML and distinguish elments based on their parents. I use Groovy and XmlSlurper.
I know that the GPathResults implements equals() as equality of text() nodes only. Sadly, thats not usable in my case.
Using cmp via is() seems to be pointless since every time you get new results object. I'm a newb in Groovy, so I don't feel like overloading the equals() method.
In this case I'd like to distinguish between those elements by their parent(). Let's say I got GPathResults of element 'b' stored in a variable. How can I get that particular element 'a' "which got that stored element 'b' as its NEAREST parent"?
def xml = ''' <root>
<a type="1"/>
<a type="2"/>
<b>
<a type="1"/>
</b>
</root>
'''.trim()
def slurper = new XmlSlurper(false, false).parseText(xml)
def myParticularB = slurper.b
def wantedA = slurper.depthFirst().find { seg ->
seg.name() == 'a' && seg.#type == '1' && seg.parent() == myParticularB
}
assert (wantedA.parent().name() == 'b') == true
I'm sorry if I overlooked something obvious.
//A corner case
<root>
<a type="1"/>
<a type="2"/>
<b>
<a type="1"/>
<b>
<a type="1"/>
<b>
<a type="1"/>
</b>
</b>
</b>
</root>
There are two JavaBean A and B. and 2 String C and D.
There structure is
A
--B
----C
----D
I can marshall Object A to Xml
<A>
<B>
<C>ccc</C>
<D>ddd</D>
</B>
</A>
And In some cases, I'd like to marshall A to XML like this
<A>
<C>ccc</C>
<D>ddd</D>
</A>
Without B tag.
can it be done by XMLAdapter, or other ways.
it' the best if it has some dynamic ways.
Thanks.
Note: I'm the EclipseLink JAXB (MOXy) lead and a member of the JAXB (JSR-222) expert group.
You could leverage MOXy's #XmlPath extension for this use case:
import javax.xml.bind.annotation.*;
import org.eclipse.persistence.oxm.annotations.XmlPath;
#XmlRootElement(name = "A")
#XmlAccessorType(XmlAccessType.FIELD)
public class A {
#XmlPath(".")
private B b;
}
For More Information
http://blog.bdoughan.com/2010/07/xpath-based-mapping.html
http://blog.bdoughan.com/2011/05/specifying-eclipselink-moxy-as-your.html
I'm trying to analyse an XML tree using XmlSlurper and GPath, and the behaviour of the findAll method confuses me.
Say, for example, that you have the following XML tree:
<html>
<body>
<ul>
<li class="odd"><span>Element 1</span></li>
<li class="even"><span>Element 2</span></li>
<li class="odd"><span>Element 3</span></li>
<li class="even"><span>Element 4</span></li>
<li class="odd"><span>Element 5</span></li>
</ul>
</body>
</html>
Assuming that xml has been initialised through one of XmlSlurper's parse methods, the following code executes as one would expect:
// Prints:
// odd
// odd
// odd
xml.body.ul.li.findAll {it.#class == 'odd'}.#class.each {println it.text()}
On the other hand:
// Doesn't print anything.
xml.body.ul.li.findAll {it.#class == 'odd'}.span.each {println it.text()}
I'm struggling to understand why I can use the special # property (as well as others, such as **), but not 'normal' ones.
I've looked at the API code, and what confuses me even more is that the getProperty implementation (found in GPathResult) seems to support what I'm trying to do.
What am I missing?
You need to iterate over every span, so you can use the spread-dot operator:
xml.body.ul.li.findAll {it.#class == 'odd'}*.span.each {println it.text()}