如何将xml加载到webview android中

4dc9hkyq  于 2022-11-03  发布在  Android
关注(0)|答案(4)|浏览(171)

我有一个xml文件,我想将xml内容加载到一个web视图中,该web视图将被放大为一个图库。我已经解析了并且能够获得解析的值,但是我如何将xml内容加载到web视图中呢?
这是我用来加载xml的代码:

public class ReadContent extends Activity implements GalleryListener {
private GalleryView myGallery;
Context mContext = null;
private ContentAdapter contentAdapter;

@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.readcontent);
    myGallery = (GalleryView) findViewById(R.id.myGallery);
    myGallery.setGalleryListener(ReadContent.this);
    myGallery.setVerticalFadingEdgeEnabled(false);
    contentAdapter = new ContentAdapter();
    myGallery.setAdapter(contentAdapter);
}

class ContentAdapter extends BaseAdapter {

    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        return Intro.book.getsecretList().size();
    }

    @Override
    public Object getItem(int arg0) {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public long getItemId(int position) {
        // TODO Auto-generated method stub
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        // TODO Auto-generated method stub
        View view = convertView;
        if (convertView == null) {
            // view = inflater.inflate(resourceid, null);
            view = getLayoutInflater().inflate(R.layout.webviewitem, null);
        }
        synchronized (view) {
            TextView txtTopic = (TextView) view
                    .findViewById(R.id.txtChapter);
            TextView txtSubTopic = (TextView) view
                    .findViewById(R.id.txtSubChapter);
            WebView wv = (WebView) view.findViewById(R.id.contentWebView);
            txtTopic.setText(Intro.book.getsecretList().get((int) position)
                    .getId().toString());
            txtSubTopic.setText(Intro.book.getsecretList()
                    .get((int) position).getId().toString());
            // webview content
            WebSettings settings = wv.getSettings();
            settings.setUseWideViewPort(true);
            settings.setLoadWithOverviewMode(true);
            settings.setJavaScriptEnabled(true);
            settings.setDefaultZoom(ZoomDensity.FAR);
            wv.setBackgroundColor(0);
            wv.setVerticalScrollBarEnabled(false);
            wv.setHorizontalScrollBarEnabled(false);

            wv.loadUrl("file:///android_asset/xxx.xml");
        }
        return view;
    }
}}

请帮我的代码,我将不胜感激。

<prelim>
    <prelimTitle>Managing people is hard but rewarding</prelimTitle>
    <prelimIntro>As you go through life, you will increasingly find that you need to manage people. A parent has to manage their family; a supervisor or team leader has to manage a small team; an entrepreneur may have to manage staff, customers and suppliers.</prelimIntro>
    <prelimInsight>Knowing how to manage people well is one of the most important skills in life.</prelimInsight>
    <prelimContent>

        <![CDATA[
        <p>Early in my career I took responsibility for managing people. I managed up to 250 highly trained professionals who worked as a tight-knit team. It didn’t matter that I was the youngest person in the team! For over 20 years I’ve been working with individuals and organizations to help them improve their management of people. This has ranged from military personnel to entrepreneurs, from charities to government departments. I’ve learned many secrets and tricks over these years. Some I’ve discovered for myself, but many I’ve learned from others. Humans are wonderfully inventive!</p>
        <p>This book aims to help you improve your skills at managing people – to help you find ways in which everybody benefits. It contains 50 secrets, grouped into seven themed chapters.</p> 
        <p>
        <ul>
        <li>
        <span class='pointHeading'>Build on a strong foundation.</span><span class='normalText'>You must understand what type of leader or manager you want to be. Your employer may give guidelines, but you must exert control over your day-to-day behaviour.</span>   
        </li>
        <li>
        <span class='pointHeading'>Create a great team.</span><span class='normalText'>This shows how to choose the right people and quickly build a functioning team.</span>   
        </li>
        <li>
        <span class='pointHeading'>Set goals and targets.</span><span class='normalText'>By setting people effective targets and goals, you can monitor progress and offer appropriate rewards.</span>   
        </li>
        <li>
        <span class='pointHeading'>Motivate yourself and your people.</span><span class='normalText'>Implementing ways to motivate people is ultimately much easier than having to cajole and constantly monitor unmotivated people.</span>   
        </li>
        <li>
        <span class='pointHeading'>Manage good performance.</span><span class='normalText'>You need to recognize good performance – reward it, develop it, perpetuate it and spread it to others.  Otherwise you will lose your good performers and be left only with the poor ones.</span>   
        </li>
        <li>
        <span class='pointHeading'>Manage poor performance.</span><span class='normalText'>Some managers find ways of managing around poor performance without tackling the poor performance itself. However, this encourages more poor performance, from both the original perpetrator and everyone else. Know how to tackle the problems head on.</span>   
        </li>
        <li>
        <span class='pointHeading'>Develop your people.</span><span class='normalText'>Though often overlooked by managers, another fundamental task is developing people. You need to improve the less able, stretch and reward the able, plan succession for the future and mentor your people’s changing needs. </span>   
        </li>
        </ul>
        </p>
        <p>Managing people is a hugely complex area in which you never stop learning. The secrets contained in this book will help you make massive strides towards succeeding in this fascinating role.</p>
        ]]>
    </prelimContent>
</prelim>
xdnvmnnf

xdnvmnnf1#

我认为您需要在标记中显示内容,
然后解析xml,并将webview显示为:

myWebView.getSettings().setJavaScriptEnabled(true);
myWebView.loadDataWithBaseURL(null, myPreviewContents, mime, encoding, null);

其中myPreviewContents是:

<p>Early in my career I took responsibility for managing people. I managed up to 250 highly trained professionals who worked as a tight-knit team. It didn’t matter that I was the youngest person in the team! For over 20 years I’ve been working with individuals and organizations to help them improve their management of people. This has ranged from military personnel to entrepreneurs, from charities to government departments. I’ve learned many secrets and tricks over these years. Some I’ve discovered for myself, but many I’ve learned from others. Humans are wonderfully inventive!</p>
        <p>This book aims to help you improve your skills at managing people – to help you find ways in which everybody benefits. It contains 50 secrets, grouped into seven themed chapters.</p> 
    <p>
    <ul>
    <li>
    <span class='pointHeading'>Build on a strong foundation.</span><span class='normalText'>You must understand what type of leader or manager you want to be. Your employer may give guidelines, but you must exert control over your day-to-day behaviour.</span>   
    </li>
    <li>
    <span class='pointHeading'>Create a great team.</span><span class='normalText'>This shows how to choose the right people and quickly build a functioning team.</span>   
    </li>
    <li>
    <span class='pointHeading'>Set goals and targets.</span><span class='normalText'>By setting people effective targets and goals, you can monitor progress and offer appropriate rewards.</span>   
    </li>
    <li>
    <span class='pointHeading'>Motivate yourself and your people.</span><span class='normalText'>Implementing ways to motivate people is ultimately much easier than having to cajole and constantly monitor unmotivated people.</span>   
    </li>
    <li>
    <span class='pointHeading'>Manage good performance.</span><span class='normalText'>You need to recognize good performance – reward it, develop it, perpetuate it and spread it to others.  Otherwise you will lose your good performers and be left only with the poor ones.</span>   
    </li>
    <li>
    <span class='pointHeading'>Manage poor performance.</span><span class='normalText'>Some managers find ways of managing around poor performance without tackling the poor performance itself. However, this encourages more poor performance, from both the original perpetrator and everyone else. Know how to tackle the problems head on.</span>   
    </li>
    <li>
    <span class='pointHeading'>Develop your people.</span><span class='normalText'>Though often overlooked by managers, another fundamental task is developing people. You need to improve the less able, stretch and reward the able, plan succession for the future and mentor your people’s changing needs. </span>   
    </li>
    </ul>
    </p>
    <p>Managing people is a hugely complex area in which you never stop learning. The secrets contained in this book will help you make massive strides towards succeeding in this fascinating role.</p>
    ]]>
qojgxg4l

qojgxg4l2#

您可以使用xsl来转换您的xml,然后将结果字符串加载到webivew中(我相信是webview.loaddata)。
有关转换xml的示例,请参见Android: Convert xml using xslt

fd3cxomn

fd3cxomn3#

一个xml文件并不总是一个网页。你必须在loadUrl()中给予一个有效的网页,如

wv.loadUrl("http://www.bbc.co.uk"");

或者使用这个是你有原始html:

String summary = "<html><body>You scored <b>192</b> points.</body></html>";
 webview.loadData(summary, "text/html", null);

(Yes,Android使用xml文件作为UI。它使用xml作为框架来扩展视图)

zpf6vheq

zpf6vheq4#

请遵循this android开发者教程中的XML解析,下载示例代码并根据您的XML文件进行修改。它首先解析XML,然后创建HTML代码字符串,最后在简单的webView中呈现此HTML字符串。

相关问题