如何更换&,<字符和& ,<在解析xml文件的android?

时间:2013-03-29 12:07:57

标签: android xml-parsing

这是我的xml文件的部分,我想在其中进行更改...... 此文件未存储在我的项目资产中,而是存储在Web上的xml文件中 我曾使用XMLPull解析器进行解析

<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>

1 个答案:

答案 0 :(得分:0)

参考:XmlPullParser

你在搜索这样的东西:

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

编辑: 以下是如何在String中获取xml:

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();
}