如何在xml中解析一个非常长的String?

时间:2012-03-15 21:13:36

标签: android xml

我有一个Parser类。它似乎只返回一定数量的字符而不是整个字符串。 它停在“&”所有我的弦乐的sybmol

e.g http://video.site.com/vidfasfasfdasdfjkdhfhafjhajsdkdhfd.mp4?Epoch=1231736487124&Policy=ew0KIlN0YXRlbWVudCI6W3sNCiJSZXNvdXJjZS

它只会返回http://video.site.com/vidfasfasfdasdfjkdhfhafjhajsdkdhfd.mp4?Epoch=123173648712

我可以进行哪些修改,以便返回整个字符串?

public class Parser extends Activity {

//No generics
private List<Video> myVideos;
private Document dom;
private String TAG = "Parser";

/*
 * Constructor
 */
public Parser(){
    //create a list to hold the video objects
    myVideos = new ArrayList<Video>();
}

/*
 * parse xml file 
 * @param File path
 */
void parseXmlFile(String path){
    //get the factory
    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();

    try {

        //Using factory get an instance of document builder
        DocumentBuilder db = dbf.newDocumentBuilder();
        //parse using builder to get DOM representation of the XML file
        dom = db.parse(path);


    }catch(ParserConfigurationException pce) {
        pce.printStackTrace();
    }catch(SAXException se) {
        se.printStackTrace();
    }catch(IOException ioe) {
        ioe.printStackTrace();
    }
}

/*
 * 
 */
public void runParser(String path){
     parseXmlFile(path);
     parseDocument();
     printData();
}


/*
 * parse DOM object
 * Pass each element to getVideo
 */
void parseDocument(){
    //get the root elememt
    Element docEle = dom.getDocumentElement();

    //get a nodelist of <video> elements

    NodeList nl = docEle.getElementsByTagName("item");
    if(nl != null && nl.getLength() > 0) {
        for(int i = 0 ; i < nl.getLength();i++) {   
            //get the video element
            Element el = (Element)nl.item(i);   
            //get the video object
            Video e = getVideo(el);

            //add it to list
            myVideos.add(e);
        }
    }
}


/*
 * I take an Video element and read the values in, create
 * an Video object and return it
 * @param Video
 * @return
 */
private Video getVideo(Element videoEle){

    //for each <Video> element get text or int values of 
    //StreamUrl , thumbNail, title , episode, description etc


    //Topmost Node elements e.g  playlist 
    Node parentNode = getParentNode(videoEle);
    Element parentElement = (Element)parentNode;


    //Node with item Element
    String streamUrl = getTextValue(videoEle,"streamUrl");
    String thumbNail = getTextValue(videoEle,"thumbnail");
    String title = getTextValue(videoEle,"title");
    String episode = getTextValue(videoEle,"episode");
    String descr = getTextValue(videoEle,"description");


    //Get element Attribute
    String playListName = parentElement.getAttribute("name");
    String linkXMLURL = videoEle.getAttribute("linkXMLURL");

    //Create a new Video object with the value read from the xml nodes
    Video e = new Video(playListName,linkXMLURL,streamUrl,thumbNail,title,episode,descr);

    return e;
}


/*
 * Take a xml element and the tag name
 * look for the tag and get the text content  
 * @param ele
 * @param tagName
 * @return String
 */
private String getTextValue(Element ele, String tagName) {
    String textVal = null;
    NodeList nl = ele.getElementsByTagName(tagName);
    if(nl != null && nl.getLength() > 0) {
        Element el = (Element)nl.item(0);
        textVal = el.getFirstChild().getNodeValue();
    }

    return textVal;
}


/*
 * Calls getTextValue
 * @param ele
 * @param tagName
 * @return int
 */
private int getIntValue(Element ele, String tagName) {
    //in production application you would catch the exception
    return Integer.parseInt(getTextValue(ele,tagName));
}


/*
 * @param ele
 * @return Node
 */
private Node getParentNode(Element ele){    
    Node parentNode = ele.getParentNode();
    return parentNode;

}

public List<Video> getList(){
    return myVideos;
}

public int getCount(){
    return myVideos.size();
}

/*
 * Iterate through the list and print the 
 * content to console
 */
void printData(){       
    System.out.println("No of Videos'" + myVideos.size() + "'.");

    Iterator<Video> it = myVideos.iterator();
    while(it.hasNext()) {
        //System.out.println(it.next().toString());
        Log.i(TAG, it.next().printURL());
    }
}

public static void main(String[] args){ 


}

}

2 个答案:

答案 0 :(得分:0)

Ampersand是XML中的特殊字符。您需要在源XML中使用&amp;

您可以找到XML特殊字符here.

的列表

答案 1 :(得分:0)

我发现如果我对Element进行了规范化,那么它的工作效果非常好。

Element.normalize() 规范化方法规范化所有子树文本节点,即,它将两个或多个相邻的节点组合成单个节点。在“正常”形式中,文本节点只能通过标记分隔,例如标记,注释,处理指令,CDATA部分和实体引用。此表单对需要特定文档结构的操作很有用,并确保在保存和重新加载时文档的DOM视图保持不变。

void parseDocument(){
//get the root elememt
Element docEle = dom.getDocumentElement();

docEle.normalize();

//get a nodelist of <video> elements

NodeList nl = docEle.getElementsByTagName("item");
if(nl != null && nl.getLength() > 0) {
    for(int i = 0 ; i < nl.getLength();i++) {   
        //get the video element
        Element el = (Element)nl.item(i);   
        //get the video object
        Video e = getVideo(el);

        //add it to list
        myVideos.add(e);
    }
}
}