Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.java.programmer > #33527
| Subject | Re: how to loop over nested yaml file content? |
|---|---|
| Newsgroups | comp.lang.java.programmer |
| References | <83fb14d1-778c-44b8-bc6a-25742445c958@googlegroups.com> <enum-20160123152735@ram.dialup.fu-berlin.de> |
| From | Daniel Pitts <newsgroup.nospam@virtualinfinity.net> |
| Message-ID | <XkNoy.139454$K21.72352@fx02.iad> (permalink) |
| Date | 2016-01-23 07:52 -0800 |
On 1/23/16 6:27 AM, Stefan Ram wrote:
> Mohammed Suleman khan <salmankhan0921@gmail.com> writes:
>> I am having a .yml file that has nested level navigation
>
> public final class Main
> {
>
> private static final java.lang.String source =
> "navigationItems:\n" +
> "-\n" +
> " link: /features\n" +
> " label: Features\n" +
> "-\n" +
> " link: /contact\n" +
> " label: Contact Us\n" +
> "-\n" +
> " link: /about\n" +
> " label: About Us\n" +
> "-\n" +
> " link: /services\n" +
> " label: Services\n" +
> " navigationItems:\n" +
> " -\n" +
> " link: /service-1\n" +
> " label: Service 1\n";
>
> public static void main(String args[]) throws Exception
> { final java.util.regex.Pattern p =
> java.util.regex.Pattern.compile
> ( "^ {6}([^ ].*)$", java.util.regex.Pattern.MULTILINE );
> final java.util.regex.Matcher m = p.matcher( source );
> while( m.find() )java.lang.System.out.println( m.group( 1 )); }}
>
> link: /service-1
> label: Service 1
>
Stefan: why are you using regex to parse Yaml? Yaml has a spec, and its
far more complex than your regex suggests.
Mohammed: The problem is that you're trying to represent a tree
structure as a list. Really you need:
class NavigationNode {
private List<NavigationNode> children;
private String label;
private String link;
}
Then you can use recursion to go through the list.
Hopefully this helps.
Back to comp.lang.java.programmer | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
how to loop over nested yaml file content? Mohammed Suleman khan <salmankhan0921@gmail.com> - 2016-01-23 06:06 -0800
Re: how to loop over nested yaml file content? Daniel Pitts <newsgroup.nospam@virtualinfinity.net> - 2016-01-23 07:52 -0800
Re: how to loop over nested yaml file content? Jeff Higgins <jeff@invalid.invalid> - 2016-01-23 16:28 +0000
csiph-web