Get components that are inside a xhtml file

Asked

Viewed 490 times

3

In a source code facelets (xhtml) of the primefaces, I want to extract all tags <p:inputText>. After that, I want to take the attribute label. Could you do that? Remembering that the components have different filled values, and I can put other tags of the primefaces as outputlabel or div.

  • 2

    Macario, you have a source code facelets (xhtml) and want to extract certain tags, that’s it?

  • @Anthonyaccioly that there! To be more precise, I wanted to extract the tags from certain components to get their Abels and generate a properties file. Understand?

1 answer

2


You need a parser. While the JDK has the Documentparser, he is legacy and hitched to the Swing Apis.

My recommendation would be Jsoup

// Trate exceções no código real
File input = new File("/tmp/input.xhtml");
Document doc = Jsoup.parse(input, "UTF-8");
Elements labels = doc.select("p|inputText[label]");
// todos os inputs que possuem um label
for (Element label : labels) {
   String sLabel = label.attr("label"); 
   System.out.println("Label: " + sLabel);
}

I am assuming that you will extract the Labels directly from the Facelets code, but if you need to extract the generated html just adapt the code accordingly.

  • In this excerpt what was used? Regex? doc.select("p|inputText[label]")

  • 1

    No, it’s a select syntax similar to jQuery. See Documentation jsoup. It searches for all tags inputText with the namespace p and containing an attribute label.

  • Just one more question, I’m not 100% sure because there are 80 xhtml pages that I want to do this, which are all, the elements that use label, which actually follow a pattern of this kind #{lbl['LABEL.CHECKLIST.ITEMDEVERIFICACAO']}, everyone has it #{lbl['']}.

  • 1

    Macario, you can use the methods of the classes Element and Attributes to iterate element attributes and filter only those that start with #{lbl[' (for example), but this is a very expensive processing (check all tags and each element of each tag), the better you can filter the elements better.

  • 1

    Man, I got it, real crazy! Elements labels = doc.select("[label*=#]");

  • Unfortunately some tags like h:inputText were not caught

  • I do not understand, because when I have selected it is the elements that label*=#, should bring everyone not?

  • You made a mistake... you can’t parse

Show 4 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.