Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7918979
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T15:41:55+00:00 2026-06-03T15:41:55+00:00

I am trying to achieve BOOLEAN return (true/false) based on XML file upload by

  • 0

I am trying to achieve BOOLEAN return (true/false) based on XML file upload by user. For instance, I have a element direction which indicates type of data it contains. So i am interested in un marshaling the data and return boolean.

Step 1: Interested in POST method and would be testing using POSTMAN chrome app.

Step 2: Contents object to hold everything for un marshaling and marshaling

package validator.service;

import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;

// Created the Contents object to hold everything for un marshaling and marshaling

@XmlRootElement( name = "contents" )
public class Contents
{
    @XmlElement
    String portalarea;

    @XmlElement
    String portalsubarea;

    @XmlElement
    String direction;

    public String getportalarea()
    {
        return portalarea;
    }

    public String getportalsubarea()
    {
        return portalsubarea;
    }

    public String getdirection()
    {
        return direction;
    }

}

Step 3: Have Validation Class for receiving the Request and un marshal the XML to return boolean.

package validatorService;

import java.io.InputStream;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBElement;
import javax.xml.bind.Unmarshaller;
import javax.xml.stream.XMLInputFactory;
import javax.xml.stream.XMLStreamReader;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;

@Path ("/valid")
public class ValidatorService
{
    boolean n_value = false;
    boolean r_value = false;

    @POST
    @Produces( MediaType.TEXT_PLAIN )
    @Consumes( "application/xml" )
    public String validate( String xmlContent )
    {
        HttpClient httpclient = new DefaultHttpClient();

        try
        {
            if ( xmlContent != null )
            {
                if ( xmlContent.startsWith( "https" ) )
                {
                    HttpGet xmlGet = new HttpGet( xmlContent );

                    HttpResponse response = httpclient.execute( xmlGet );
                    int responseStatus = response.getStatusLine().getStatusCode();
                    // String responseMessage = response.getStatusLine().getReasonPhrase();

                    if ( responseStatus == 200 )
                    {
                        HttpEntity responseEntity = response.getEntity();
                        InputStream inStream = responseEntity.getContent();

                        Contents direction = unmarshalingContent( inStream, xmlContent );

                        if ( direction.equals( "N" ) )
                        {
                            n_value = true;


                        }
                        else if ( direction.equals( "R" ) )
                        {
                            r_value = true;


                    }
                    else
                    {
                        System.out.println( "Response Error : " + responseStatus ); // Should be
                                                                                    // handled
                                                                                    // properly
                    }

                }
                else
                {
                    System.out.println( " 'https' Format Error" ); // Should be handled properly
                }

                return "success";
            }

        }
        catch ( Exception e )
        {
            e.printStackTrace();
            System.out.println( " Error caught at catch " + e ); // Should be handled properly for
                                                                 // all exception
        }
        finally
        {
            httpclient.getConnectionManager().shutdown();
        }

        return null;
    }

    public Contents unmarshalingContent( InputStream inputStream, String resourceClass ) throws Exception
    {
        System.out.println( " welcome " );

        if ( resourceClass == "xmlContent" )
        {
            JAXBContext jc = JAXBContext.newInstance( "com.acme.bar" );
            Unmarshaller u = jc.createUnmarshaller();

            XMLInputFactory inputFactory = XMLInputFactory.newInstance();
            XMLStreamReader xReader = inputFactory.createXMLStreamReader( inputStream );

            JAXBElement<Contents> jaxBElement = (JAXBElement<Contents>) u.unmarshal( xReader, Contents.class );

            Contents portalArea = (Contents) jaxBElement.getValue();
            Contents portalSubarea = (Contents) jaxBElement.getValue();
            Contents direction = (Contents) jaxBElement.getValue();

            return direction;
        }
        throw new Exception( "Invalid resource request" );

    }
}

I am new to RESTful Service and i read few documents and based on instructions i am trying to achieve the given task. So any help, corrections, guidance, code is much appreciated.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-03T15:41:57+00:00Added an answer on June 3, 2026 at 3:41 pm

    It can be much simpler. You don’t need to do XML to Java object conversion by hand. JAX-RS providers are doing that automatically.

    @POST
    @Produces( MediaType.TEXT_PLAIN )
    @Consumes( "application/xml" )
    public Response validate(Contents con){ //con will be initialized by JAX-RS
      //validate your XML converted to object con
      boolean validation_ok = ...
      if(validation_ok){
         return Response.ok("true").build();
      }else{
         return Response.ok("false").build();
      }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Trying to achieve any moving effect while appending an element from one to another
im trying to achieve something but i dont really know how I have set
I am trying to achieve an LDAP query to gather all properties we have
I'm trying to have a button that brings up an upload dialog. The way
Trying to achieve the goal of printing selected page's element ONE-TO-ONE - the exact
Trying to achieve something like this using recursion: if (m > n) return; Foo
Im trying to achieve the following: A certain page will have a series of
Here is what I am trying to achieve I have a function to generate
I'm trying to have my Activity update an ImageView whenever a boolean value changes
I have this main.xml file: <?xml version=1.0 encoding=utf-8?> <LinearLayout xmlns:android=http://schemas.android.com/apk/res/android android:orientation=vertical android:layout_width=fill_parent android:layout_height=fill_parent android:background=@drawable/background

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.