2012-03-15 17 views
0

我有一個解析器類。它似乎只返回一定數量的字符而不是整個字符串。 它停在「&」 sybmol我所有的字符串如何在xml中解析非常長的字符串?

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


} 

}

回答

0

列表我發現,如果我歸元素是完全正常工作。

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

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

你可以找到的XML特殊字符here.

+0

我無法修改源文件。如何指示我的解析器忽略他的特殊字符? – Fabii 2012-03-15 21:22:18

+0

您可能需要使用SAX解析器。 – 2012-03-15 21:31:11

+0

您的源文件不是XML,因此不要嘗試使用XML解析器解析它。您或者需要更改系統,以便傳輸適當的格式良好的XML,或者您需要定義自己的非XML語言並編寫自己的工具來處理它。我知道我會選擇哪條路線。 – 2012-03-15 23:17:08