Do this:
writer.write(essai.outerHtml());
instead of
writer.write(doc.html());
Question
I'm trying to parse a phtml file from a project and save modifications to this file with Java. I actually use Jsoup API to parse this file. My problem is the saving step. Lets see an example :
PHTML FILE :
<div id="header">
<div id="logo">
<img src="images/logo.png" id="logo_pic" />
</div>
<div id="welcome">
<span id="welcome_title">
</span>
</div>
</div>
Java parsing :
File testFile = new File("C:\\Users\\root\\Desktop\\test.phtml");
try {
Document doc = Jsoup.parse(testFile, "UTF-8");
Element essai = doc.getElementById("welcome_title");
essai.appendText("Application NAME!");
PrintWriter writer = new PrintWriter(testFile, "UTF-8");
writer.write(doc.html());
writer.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
Result i want to have should be like this :
<div id="header">
<div id="logo">
<img src="images/logo.png" id="logo_pic" />
</div>
<div id="welcome">
<span id="welcome_title">
Application NAME!
</span>
</div>
</div>
Result i have for the moment :
<html>
<head></head>
<body>
<div id="header">
<div id="logo">
<img src="images/logo.png" id="logo_pic" />
</div>
<div id="welcome">
<span id="welcome_title"> Application NAME!</span>
</div>
</div>
</body>
</html>
My problem is that i must keep a phtml format with only div elements for my application. How can i reach the good result? Any idea? Thx for help!
Solution
Do this:
writer.write(essai.outerHtml());
instead of
writer.write(doc.html());