How to extract element of XML file using apache axiom which is mainly use in WSO2 products

In our projects we need to extract elements from xml files. later i was use some other jar that allows xml manipulation. but when started working with WSO2 i have to use axiom.Now I'm comfortable with it and decide to put this post about it

<packages xmlns="http:tempuri.org/">
    <package name="free">
        <users>
            <limit>5</limit>
            <charge>0</charge>
        </users>
        <resourceVolume>
            <limit>10</limit>
        </resourceVolume>
        <bandwidth>
            <limit>1000</limit>
            <overuseCharge>0</overuseCharge>
        </bandwidth>
    </package>
    <package name="small">
        <users>
            <limit>10</limit>
            <charge>10</charge>
        </users>
        <resourceVolume>
            <limit>25</limit>
        </resourceVolume>
        <bandwidth>
            <limit>2000</limit>
            <overuseCharge>0.1</overuseCharge>
        </bandwidth>
        <userLimit>10</userLimit>
    </package>
</packages>

just assume that we have XML in above format.its about packages of service provider.so we may need to get packages objects and extract the

name of each package
users limit
resourceVolume limit
bandwidth limit

So we will see how to extract those elements.in my code first while loop iterates through the objects and second while loop iterates through the elements of each object.

String configFileName = "path_to_file/file_name.xml";
if (new java.io.File(configFileName).exists())
{
    //if file exsists then only proceeds
    try
    {
    //create OMElement from the file given
    org.apache.axiom.om.OMElement userConfigRoot =
    org.wso2.stratos.common.util.CommonUtil.buildOMElement(new java.io.FileInputStream(configFileName));
    //iterates through the elements of the file in this case we will go through package elements
    java.util.Iterator userConfigChildIt = userConfigRoot.getChildElements();
       while (userConfigChildIt.hasNext())
    //this reads through all elements in the file till end
       {
          Object userConfigChild = userConfigChildIt.next();
    //get element of package         
        if (!(userConfigChild instanceof OMElement))
               {
        //if that is not OMElement we will skip
              continue;
               }
       OMElement meteringConfigChildEle = (OMElement) userConfigChild;
       String parameterValue=
       meteringConfigChildEle.getAttributeValue(new javax.xml.namespace.QName(null, "name"));
    //get the variable name by parameter name in this case we will extract name parameter of the
    //package object.we can use string value of given parameter name
       Iterator userParametersChildIt = meteringConfigChildEle.getChildElements();
       //again iterates through the sub elements of the package object
              while (userParametersChildIt.hasNext())
              {
             param_number=param_number+1;
             Object userParameterChild = userParametersChildIt.next();
                 if (!(userParameterChild instanceof OMElement))
        {
                    continue;
                }
              OMElement userParameterChildEle = (OMElement) userParameterChild;
             if(userParameterChildEle.getFirstElement()!=null)
        {
         //in this we will get the elements of the object
                 String temp= userParameterChildEle.getFirstElement().getText();
                }
           }
        }

No comments:

Post a Comment

Empowering the Future of API Management: Unveiling the Journey of WSO2 API Platform for Kubernetes (APK) Project and the Anticipated Alpha Release

  Introduction In the ever-evolving realm of API management, our journey embarked on the APK project eight months ago, and now, with great a...