2013-05-27 165 views
0

請問,我想知道我們如何從xsl文件中獲得java項目列表。如何從XSL獲取項目列表

這是文件的一個例子:

//catalog.xml

<catalog> 
    <cd> 
     <title>Empire Burlesque</title> 
     <artist>Bob Dylan</artist> 
     <country>USA</country> 
    </cd> 
    <cd> 
     <title>Hide your heart</title> 
     <artist>Bonnie Tyler</artist> 
     <country>UK</country> 
    </cd> 
    <cd> 
     <title>Greatest Hits</title> 
     <artist>Dolly Parton</artist> 
     <country>USA</country> 
    </cd> 
</catalog> 

// XSL文件中我有:

<xsl:for-each select="catalog/cd"> 
     <tr> 
     <td><xsl:value-of select="title" /></td> 
     <td><xsl:value-of select="artist" /></td> 
     </tr> 
</xsl:for-each> 

所以現在我該怎麼辦如果我想獲得java類中所有標題的列表?

換句話說,我想得到(在java類中)包含3個值的列表:帝國滑稽表演,隱藏你的心臟和最偉大的命中。

感謝您提前

+0

謝謝你的迴應。事實上,我是XSLT新手,所以我沒有太多的如何解決這個問題 – Samounas

+0

爲了在Java中提取這些值,可以簡單地使用JAXP/JAXB。 –

+0

謝謝。請你能給我一個這樣的代碼的例子嗎? – Samounas

回答

0

也許你可以嘗試使用XPath。您可以查詢節點的節點列表,並打通節點列表

喜歡的東西迭代名稱:

public NodeList nodeListByXPath(Node xml, 
           String theXPath) throws XPathExpressionException { 
NodeList outNodes = (NodeList)_xPath(xml,theXPath,XPathConstants.NODESET); 
    return outNodes; 
} 
private Object _xPath(Node xml, 
        String theXPath, 
       QName returnType) throws XPathExpressionException { 
    Object outObj = null; 
    XPathFactory xPathFactory = XPathFactory.newInstance(); 
    XPath xPath = xPathFactory.newXPath(); 
    XPathExpression xPathExpr = xPath.compile(theXPath.trim()); 
    if (returnType == XPathConstants.BOOLEAN) { 
     outObj = xPathExpr.evaluate(xml,XPathConstants.BOOLEAN); 
    } else if (returnType == XPathConstants.NUMBER) { 
     outObj = xPathExpr.evaluate(xml,XPathConstants.NUMBER); 
    } else if (returnType == XPathConstants.STRING) { 
     outObj = xPathExpr.evaluate(xml,XPathConstants.STRING); 
    } else if (returnType == XPathConstants.NODE) { 
     outObj = xPathExpr.evaluate(xml,XPathConstants.NODE); 
    } else if (returnType == XPathConstants.NODESET) { 
     outObj = xPathExpr.evaluate(xml,XPathConstants.NODESET); 
    } 
    return outObj; 
} 
public Document parse(InputStream is) throws SAXException { 
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); 
    factory.setIgnoringComments(true);  
    factory.setValidating(false);   
    factory.setIgnoringElementContentWhitespace(true); 
    factory.setNamespaceAware(false);    
    DocumentBuilder builder; 
    try { 
     builder = factory.newDocumentBuilder(); 
     InputSource ins = new InputSource(is); 
     Document doc = builder.parse(ins);  
     return doc; 
    } catch (ParserConfigurationException pcEx) { 
     throw new SAXException(pcEx); 
    } catch (IOException ioEx) { 
     throw new SAXException(ioEx); 
    } 
} 

您只需撥打:

Document doc = parse(is); 
NodeList nl = nodeListByXPath(doc.getDocumentElement(), 
           "/catalog/cd/title"); 

現在通過節點迭代

+0

非常感謝您的幫助。請問我有一個問題「是」是哪個文件的輸入流? – Samounas

+0

是的,是一個InputStream的xml源 – futuretelematics

+0

好吧,非常感謝你:) – Samounas