0

I have a .html file with a content like that:

<ul class="myclass">
  <li>PLACEHOLDER</li>
</ul>

I also have an ArrayList like this:

["value 1", "value 2", "value 3"]

Now I want to create a new html file with li-entries for all of my elements in this list. So I expect this as a result:

<ul class="myclass">
  <li>value 1</li>
  <li>value 2</li>
  <li>value 3</li>
</ul>

I could first loop over my List and create a String like "<li>value 1</li><li>value 2</li><li>value 3</li>" and do a replace on "<li>PLACEHOLDER</li>" - but I hope there is better solution.

4
  • What plataform you are using? Java EE? JSF? What? Commented Sep 28, 2016 at 14:17
  • @ Dante Faña Badia: JSE Commented Sep 28, 2016 at 14:22
  • Have a look on this using xslt template Commented Sep 28, 2016 at 14:29
  • @Munchkin if you are using Java SE the simple solution is what you said replace the string or you can use (or create) a template language. Commented Sep 28, 2016 at 14:37

2 Answers 2

1

Looping works fine, but rather than building the entire string then replacing, you can use StringBuilder to concatenate the thing in pieces.

https://docs.oracle.com/javase/7/docs/api/java/lang/StringBuilder.html

First add the < ul class="myclass">, then instead of < li>PLACEHOLDER< /li>, loop through your array and append < li>{value}< /li>, etc.

Sign up to request clarification or add additional context in comments.

Comments

1

if you are using Java SE the simple solution is what you said replace the string or you can use (or create) a template language Or you can use a existing this is one maked by Apache http://freemarker.org/ (I have never use)

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.