1

I have this XML :

<rdf:RDF 
xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
xmlns="http://purl.org/rss/1.0/">
<channel rdf:about="http://www.web.com/news.rss">
<items>
<rdf:Seq>
    <rdf:li resource="http://www.web.com/news1" />
    <rdf:li resource="http://www.web.com/news2" />
 </rdf:Seq>
</items>
</channel>
<item rdf:about="http://www.web.com/news1">
<title>my title</title>
<link>http://www.web.com/news1.html</link>
<description> bla bla </description>
</item>
<item rdf:about="http://www.web.com/news2">
<title>My Title 2</title>
<link>http://www.web.com/news2.html</link>
<description> bla bla 2 </description>
</item>
</rdf:RDF>

Here's my code to try to print title from each rdf:li

public static void main(String[] args) throws Exception {
System.setOut(new PrintStream(System.out, true, "Cp850")); 
    XPath xPath =  XPathFactory.newInstance().newXPath();
   DocumentBuilderFactory factory = 
    DocumentBuilderFactory.newInstance();
   DocumentBuilder parser = factory.newDocumentBuilder();
   Document doc = parser.parse(args[0]);

   Element racine = doc.getDocumentElement(); 
       System.out.println("racine: "+ racine);
   NodeList nl = racine.getElementsByTagName("rdf:li");
for (int k = 0; k < nl.getLength(); ++k) {
    Element e = (Element) nl.item(k);
    String NewsTitle = e.getAttribute("resource");
         System.out.println("from :" + NewsTitle);
     String NoTitre = xPath.compile("//item[@rdf:about='" + NewsTitle + "']/title").evaluate(doc);
              System.out.println("Title : "+ NoTitre );

    }
}

Everything goes ok inside my loop, my NewsTitle variable gives me the good result.

My problem is my xpath.Compile is not good, I need help to know how to call my xpath to find the title for each news that will gives this result :

from : http://www.web.com/news1
Title : bla bla
from : http://www.web.com/news2
Title : bla bla 2

I'm not able to get the line in bold.(Title : bla bla)

thanks

1

1 Answer 1

-1

Thank I found finally by myself. The Xpath need to remove the namespace rdf: giving this :

String NoTitre = xPath.compile("//item[@about='" + NouvelleTitre + "']/title").evaluate(doc);
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.