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 8565065
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T17:20:21+00:00 2026-06-11T17:20:21+00:00

I need to parse a xml file (I do parse using JAXB) , but

  • 0

I need to parse a xml file (I do parse using JAXB) , but i want to take some part of the Xml file value into a string. After i surfed in net, it can be possible by using CDATA. The follwing link talks about CDATA. But i do have a doubt in the code. Where and how should i specify which of my elements i want to be handled as CDATA.

http://jaxb.java.net/faq/JaxbCDATASample.java

My XML look like the following.

<root>
    <Service>CDATASERVICE</Service>
    <Child>
        <param>
            <value>
                <struct>
                    <member>
                        <name>Servicename</name>
                        <value>service1</value>
                    </member>
                </struct>
            </value>
        </param>
        <param>
            <value>
                <struct>
                    <member>
                        <name>item1</name>
                        <value>36</value>
                    </member>
                    <member>
                        <name>Cdata</name>
                        <value>
                            <struct>
                                <member>
                                    <name>services</name>
                                    <value>
                                        <array>
                                            <data>
                                                <value>ABCD</value>
                                            </data>
                                        </array>
                                    </value>
                                </member>
                            </struct>
                        </value>
                    </member>
                    <member>
                        <name>item2</name>
                        <value>
                            <int>10</int>
                        </value>
                    </member>
                </struct>
            </value>
        </param>
    </Child>
</root>

I want to take the following part as a string from the above XML file, how can i get that.

                 <name>Cdata</name>
                            <value>
                                <struct>
                                    <member>
                                        <name>services</name>
                                        <value>
                                            <array>
                                                <data>
                                                    <value>ABCD</value>
                                                </data>
                                            </array>
                                        </value>
                                    </member>
                                </struct>
                            </value>
  • 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-11T17:20:22+00:00Added an answer on June 11, 2026 at 5:20 pm

    You can use an @XmlAnyElement with a DomHandler specified for this use case.

    MemberHandler

    An @XmlAnyElement annotation allows you to keep portions of an XML document as XML. By default this is kept as DOM nodes. By implementing a DomHandler you can leverage an alternate representation such as a String.

    package forum12512299;
    
    import java.io.*;
    import javax.xml.bind.ValidationEventHandler;
    import javax.xml.bind.annotation.DomHandler;
    import javax.xml.transform.Source;
    import javax.xml.transform.stream.*;
    
    public class MemberHandler implements DomHandler<String, StreamResult> {
    
        private static final String MEMBER_START_TAG = "<member>";
        private static final String MEMBER_END_TAG = "</member>";
    
        private StringWriter xmlWriter;
    
        public StreamResult createUnmarshaller(ValidationEventHandler errorHandler) {
            xmlWriter = new StringWriter();
            return new StreamResult(xmlWriter);
        }
    
        public String getElement(StreamResult rt) {
            String xml = rt.getWriter().toString();
            int beginIndex = xml.indexOf(MEMBER_START_TAG) + MEMBER_START_TAG.length();
            int endIndex = xml.indexOf(MEMBER_END_TAG);
            return xml.substring(beginIndex, endIndex);
        }
    
        public Source marshal(String n, ValidationEventHandler errorHandler) {
            try {
                String xml = MEMBER_START_TAG + n.trim() + MEMBER_END_TAG;
                StringReader xmlReader = new StringReader(xml);
                return new StreamSource(xmlReader);
            } catch(Exception e) {
                throw new RuntimeException(e);
            }
        }
    
    }
    

    Struct

    Below is an example of how the DomHandler is referenced from an @XmlAnyElement mapping.

    package forum12512299;
    
    import java.util.List;
    import javax.xml.bind.annotation.*;
    
    @XmlRootElement
    public class Struct {
    
        private List<String> members;
    
        @XmlAnyElement(MemberHandler.class)
        public List<String> getMembers() {
            return members;
        }
    
        public void setMembers(List<String> members) {
            this.members = members;
        }
    
    }
    

    input.xml

    Below I’ve simplified your XML document down to the interesting part:

    <struct>
        <member>
            <name>item1</name>
            <value>36</value>
        </member>
        <member>
            <name>Cdata</name>
            <value>
                <struct>
                    <member>
                        <name>services</name>
                        <value>
                            <array>
                                <data>
                                    <value>ABCD</value>
                                </data>
                            </array>
                        </value>
                    </member>
                </struct>
            </value>
        </member>
        <member>
            <name>item2</name>
            <value>
                <int>10</int>
            </value>
        </member>
    </struct>
    

    Demo

    package forum12512299;
    
    import java.io.File;
    import javax.xml.bind.*;
    
    public class Demo {
    
        public static void main(String[] args) throws Exception {
            JAXBContext jc = JAXBContext.newInstance(Struct.class);
    
            Unmarshaller unmarshaller = jc.createUnmarshaller();
            File xml = new File("src/forum12512299/input.xml");
            Struct struct = (Struct) unmarshaller.unmarshal(xml);
    
            for(String string : struct.getMembers()) {
                System.out.println(string);
            }
        }
    
    }
    

    Output

    <name>item1</name><value>36</value>
    
    <name>Cdata</name><value><struct><member><name>services</name><value><array><data><value>ABCD</value>
                                </data>
                            </array>
                        </value>
    
    <name>item2</name><value><int>10</int>
            </value>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to parse an XLIFF file using C#, but I'm having some trouble.
I have an XML file which I need to parse using PHP and send
I need to parse a XML String so I can use some of the
I need to parse a xml file using JAVA and have to create a
Hi I need to parse XML file using jquery. I created read and display
I am using php to parse an XML file, but I get an error
I need to parse a xml file using jQuery from an external domain. How
I need to be able to parse an xml file inside photoshop, using javascript.
I don't need to parse the XML, or to decode it into an ActionScript
I need to parse a value from an xml and place it to image's

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.