简体   繁体   中英

How to access xml element within another xml element (JAXB bindingsl

So I have an xsd schema, there can be a single user with a set of attributes, but this user may have many shares (I'm making a stock brokering app), when a user purchases a share, I unmarshall the XML into a list, I can get and set the users attributes, but I can only get a share object which I cannot set, update, delete.

Any ideas?

I assume that your question is related to this one .

Generating the classes based on your schema you would create an Accounts class.

The Accounts class contains a list for which there is no setter. Check this answer for a detailed explanation on why. But still you can do things with this list.

So when unmarshalling an xml let's say you end up with an Accounts instance and you want to add a new share. This would look like this:

Accounts accounts = unmarshallAccounts();
Accounts.Shares shares = new Accounts.Shares();
shares.setAmount("amount");
shares.setCompany("company");
accounts.getShares().add(shares);

Or did I miss-understood your question?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM