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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T06:53:08+00:00 2026-06-12T06:53:08+00:00

i have a parser here: package lt.prasom.functions; import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.StringReader; import

  • 0

i have a parser here:

package lt.prasom.functions;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.StringReader;
import java.io.StringWriter;
import java.io.UnsupportedEncodingException;
import java.util.Properties;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;
import org.w3c.dom.CharacterData;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;

import android.annotation.TargetApi;
import android.media.MediaRecorder.OutputFormat;
import android.util.Log;

public class XMLParser {

    // constructor
    public XMLParser() {

    }

    /**
     * Getting XML from URL making HTTP request
     * @param url string
     * */
    public String getXmlFromUrl(String url) {
        String xml = null;

        try {
            // defaultHttpClient
            DefaultHttpClient httpClient = new DefaultHttpClient();
            HttpGet httpGet = new HttpGet(url);

            HttpResponse httpResponse = httpClient.execute(httpGet);
            HttpEntity httpEntity = httpResponse.getEntity();
            xml = EntityUtils.toString(httpEntity);

        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } catch (ClientProtocolException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        // return XML
        return xml;
    }

    /**
     * Getting XML DOM element
     * @param XML string
     * */
    public Document getDomElement(String xml){
        Document doc = null;
        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
        dbf.setValidating(false);

        try {

            DocumentBuilder db = dbf.newDocumentBuilder();

            InputSource is = new InputSource();
                is.setCharacterStream(new StringReader(xml));
                doc = db.parse(is); 

            } catch (ParserConfigurationException e) {
                Log.e("Error: ", e.getMessage());
                return null;
            } catch (SAXException e) {
                Log.e("Error: ", e.getMessage());
                return null;
            } catch (IOException e) {
                Log.e("Error: ", e.getMessage());
                return null;
            }

            return doc;
    }

    /** Getting node value
      * @param elem element
      */
     @TargetApi(8)
    public final String getElementValue( Node elem , boolean html) {
         Node child;
         if( elem != null){
             if (elem.hasChildNodes()){
                 for( child = elem.getFirstChild(); child != null; child = child.getNextSibling() ){
                     if( child.getNodeType() == Node.TEXT_NODE  ){


                         //return child.getNodeValue();
                         return child.getNodeValue();
                     }
                 }
             }
         }
         return "";
     }

     /**
      * Getting node value
      * @param Element node
      * @param key string
      * */

     public String getValue(Element item, String str) {     
            NodeList n = item.getElementsByTagName(str);    

            return this.getElementValue(n.item(0), false);
        }

}

And there’s my sample xml :

<items>
<item>
<name>test</name>
<description>yes <b>no</b></description>
</item>
</items>

When i parse description i’m getting everything to tag (“yes”). So i want to parse raw data in description tag. I tried CDATA tag didin’t worked. Is it any way without encoding xml?

Thanks!

  • 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-12T06:53:09+00:00Added an answer on June 12, 2026 at 6:53 am

    I agree with the comments about this question not being complete, or specific enough for a direct answer (like modifying your source to work etc.), but I had to do something somewhat similar (I think) and can add this. It might help.

    So if, IF, the content of the “description” element were valid XML all by itself, so say the document actually looked like:

    <items>
      <item>
       <name>test</name>
       <description><span>yes <b>no</b></span></description>
      </item>
    </items>
    

    then you could hack out the content of the “description” element as a new XML Document and then get the XML text form that which would look then like:

    <span>yes <b>no</b></span>
    

    So a method something like:

    /**
     * Get the Description as a new XML document
     *
     */
    public Document retrieveDescriptionAsDocument(Document sourceDocument) {
    
    Document document;
    Node tmpNode;
    Document document2 = null;
    
    try {
        // get the description node, I am just using XPath here as it is easy
        // to read, you already have a reference to the node so just continue as you
        // were doing for that, bottom line is to get a reference to the node
        tmpNode = org.apache.xpath.XPathAPI.selectSingleNode(sourceDocument,"/items/item/description");
    
        if (tmpNode != null) {
    
            // create a new empty document
            document2 = DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument();
            // associate the node with the original document
            sourceDocument.importNode(tmpNode, true);
            // create a document fragment from the original document
            DocumentFragment df = sourceDocument.createDocumentFragment();
            // append the node you found, to the fragment   
            df.appendChild(tmpNode);
            // create the Node to append to the new DOM
            Node importNode = document2.importNode(df,true);
            // append the fragment (as a node) to the new empty document
            Document2.appendChild(importNode);
        }
        else {
            // LOG WARNING
            yourLoggerOrWhatever.warn("retrieveContainedDocument: No data found for XPath:" + xpathP);
        }
    
        } catch (Exception e) {
            // LOG ERROR
            yourLoggerOrWhatever.error("Exception caught getting contained document:",e);
        }
    
        // return the new doc, and the caller can then output that new document, that will now just contain "<span>yes <b>no</b></span>" as text, apply an XSL or whatever
        return document2;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is it possible to have an argument parser like this? import argparse parser.add_argument('query2target.bam', help='A
Java, ANTLR and Netbeans newbie here. I have installed a jdk and netbeans. I
I have a problem with this code right here: - (void)fetchedData:(NSData *)responseData { //parse
Well, i have written a simple python program that parses HTML with HTMLParser. Here
I have a parser that we implemented and I want to compare it to
I have a parser with me generated from yacc/lex. It is working fine for
I have a buggy xml that contains empty attributes and I have a parser
I have the following parser grammar (this is a small sample): expr: ident assignop
In my google maps program I have simple KML parser which retrieve only coordinates
the code in my app.coffee it uses coffee script i have put body parser

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.