Nikhil Sathawara
Nikhil Sathawara

Reputation: 161

How to replace &,< characters to & , < in parsed xml file for android?

Here Is my xml file's portion in which i want to make changes...... This file is not stored in my project's assets it is xml file on web I had used XMLPull parser for parsing

<exclusion_criteria>
Poor condition (centre determined), acute leukemia in relapse (&lt10% blasts), second transplants, active infection, HIV infection, T-cell antibody prophylaxis (antithymocyte globulin, anti-CD52 etc), use of cord blood grafts, T-cell depletion of grafts
            </exclusion_criteria>
        </report>

        <report>

            <tumour_disease>
Bone Marrow Transplants
            </tumour_disease>

            <trial_status>
Active- Recruiting
            </trial_status>

            <short_title>
Haplo Transplants
            </short_title>

            <title>
Haploidentical donor stem cell transplantatation for myeloid malignancies - a phase I/II pilot study in an Australian population
            </title>

            <trial_register_number>
            </trial_register_number>

            <sponsors>
St Vincents Hospital
            </sponsors>

            <trial_phase>
Phase I/II
            </trial_phase>

            <locations>
St Vincents Hospital~
            </locations>

            <lead_site>
St Vincents Hospital
            </lead_site>

            <inclusion_criteria>
Patients aged 16-50 years with haematological malignancy requiring stem cell transplantation (acute myeloid leukaemia in CR1 or CR2, intermediate-high risk myelodysplasia, chronic myeloid leukaemia in CP2-resistant to TKIs, relapsed and refractory lymphoproliferative diseases or Hodgkins lymphomas) lacking a fully HLA-matched donor; with a partially (=5/6) HLA-mismatched donor; adequate organ function (cardiac, pulmonary, renal & hepatic 
            </inclusion_criteria>

            <exclusion_criteria>
Life expectancy < 3months; psychiatric conditioning impairing provision of informed consent; active malignant disease (excluding BCC and SCC; receiving concurrent investigational drugs; pregnant or lactating women
            </exclusion_criteria>

Upvotes: 2

Views: 1582

Answers (1)

vilpe89
vilpe89

Reputation: 4702

Referring to this: XmlPullParser

Are you searching something like this:

if(eventType == XmlPullParser.TEXT) 
{
    String text = xpp.getText();
    if( text.contains("&") )
        text = text.replace("&", "&amp;");
    if( text.contains("<" ) )
        text = text.replace("<", "&lt;");
}

EDIT: Here's how you can fetch the xml in a String:

String xmlStr = "";

HttpGet uri = new HttpGet(params[0]);

DefaultHttpClient client = new DefaultHttpClient();

HttpResponse resp = client.execute(uri);
StatusLine status = resp.getStatusLine();
if( status.getStatusCode() == 200 )
{
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    DocumentBuilder builder = factory.newDocumentBuilder();
    Document doc = builder.parse(resp.getEntity().getContent());

    TransformerFactory transformerfactory = TransformerFactory.newInstance();
    Transformer transformer = transformerfactory.newTransformer();
    transformer.setOutputProperty(OutputKeys.INDENT, "yes");

    StringWriter stringWriter = new StringWriter();
    transformer.transform(new DOMSource(doc), new StreamResult(stringWriter));  

    // Now you can fix the invalid characters in your xml and put it in the parser  
    xmlStr = stringWriter.toString();
}           

Upvotes: 1

Related Questions