How to read properties from a node in CQ5?

So, you have resolved to a node and want to read the properties that exist on that node. This is pretty easy task. You should read properties with multiple values separately , if you just choose to put the below else if condition in your code, then first value of the properties with multiple value would be fetched. If don’t expect multiple value properties on your node then you can skip the first if.

 Node node= resource.adaptTo(Node.class);  
 for(PropertyIterator propeIterator = node.getProperties() ; propeIterator.hasNext();)  
 {  
      Property prop= propeIterator.nextProperty();  
      if(prop.isMultiple()) // This condition checks for properties whose type is String[](String array)  
      {  
           Property propVal = node.getProperty(prop.getName());     
           Value[] values = propVal.getValues();  
           for(Value val: values){  
           log.debug(val.getString()); // this will output the value in string format  
      }  
      }else if(!prop.getDefinition().isMultiple()){  
           log.debug(prop.getName());  
           log.debug(prop.getValue().getString());  
      }  
 }  
Advertisements

One thought on “How to read properties from a node in CQ5?

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s