Добавил:
nyan
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз:
Предмет:
Файл:
import java.io.File;
import java.text.DateFormat;
import java.text.NumberFormat;
import java.util.*;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import javax.xml.transform.stream.StreamSource;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
public class Main {
public static void main(String[] args) throws Exception {
String lang = "en";
String country = "UK";
Locale l = new Locale(lang, country);
ResourceBundle r = ResourceBundle.getBundle("Bundle", l);
DateFormat fmt = DateFormat.getDateTimeInstance(DateFormat.MEDIUM, DateFormat.MEDIUM, l);
NumberFormat currFormat = NumberFormat.getInstance(l);
DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder();
Document document = documentBuilder.newDocument();
Element recipes = document.createElement("recipes");
document.appendChild(recipes);
//блюдо 1
Element recipe1 = document.createElement("recipe");
recipes.appendChild(recipe1);
recipe1.setAttribute("name", document.createTextNode("Блины").getData());
recipe1.setAttribute("preptime", currFormat.format(5));
recipe1.setAttribute("cooktime", currFormat.format(180));
recipe1.setAttribute("editDate", fmt.format(new Date(116, 0, 12, 12, 44)));
//ингредиенты
Element ingredient1 = document.createElement("ingredient");
recipe1.appendChild(ingredient1);
ingredient1.setAttribute("amount", currFormat.format(5000));
ingredient1.setAttribute("unit", r.getString("str1"));
ingredient1.appendChild(document.createTextNode("Мука блинная"));
Element ingredient2 = document.createElement("ingredient");
recipe1.appendChild(ingredient2);
ingredient2.setAttribute("amount", currFormat.format(5));
ingredient2.setAttribute("unit", r.getString("str2"));
ingredient2.appendChild(document.createTextNode("Яйцо куриное"));
Element ingredient3 = document.createElement("ingredient");
recipe1.appendChild(ingredient3);
ingredient3.setAttribute("amount", currFormat.format(1));
ingredient3.setAttribute("unit", r.getString("str3"));
ingredient3.appendChild(document.createTextNode("Молоко"));
Element ingredient4 = document.createElement("ingredient");
recipe1.appendChild(ingredient4);
ingredient4.setAttribute("amount", currFormat.format(1));
ingredient4.setAttribute("unit", r.getString("str4"));
ingredient4.appendChild(document.createTextNode("Соль"));
//Приборы
Element dishes1= document.createElement("dishes");
recipe1.appendChild(dishes1);
dishes1.appendChild(document.createTextNode("Миска"));
Element dishes2= document.createElement("dishes");
recipe1.appendChild(dishes2);
dishes2.appendChild(document.createTextNode("Сковородка"));
Element dishes3= document.createElement("dishes");
recipe1.appendChild(dishes3);
dishes3.appendChild(document.createTextNode("Миксер"));
//Пошаговая инструкция
Element instructions1= document.createElement("instructions");
recipe1.appendChild(instructions1);
Element step1= document.createElement("step");
instructions1.appendChild(step1);
step1.appendChild(document.createTextNode("Просеять муку и соль в миску."));
Element step2= document.createElement("step");
instructions1.appendChild(step2);
step2.appendChild(document.createTextNode("Добавить молока и перемешать."));
Element step3= document.createElement("step");
instructions1.appendChild(step3);
step3.appendChild(document.createTextNode("Перемешать, пока тесто не станет однородным."));
Element step4= document.createElement("step");
instructions1.appendChild(step4);
step4.appendChild(document.createTextNode("Разогреть немного масла на сковородке. Влить на сковородку 2-3 ст.ложки теста."));
Element step5= document.createElement("step");
instructions1.appendChild(step5);
step5.appendChild(document.createTextNode("Обжарить с одной стороны до золотистой корочки."));
Element step6= document.createElement("step");
instructions1.appendChild(step6);
step6.appendChild(document.createTextNode("Перевернуть и готовить с другой сторону около 30 секунд."));
TransformerFactory transformerFactory = TransformerFactory.newInstance();
Transformer transformer = transformerFactory.newTransformer();
transformer.setOutputProperty(javax.xml.transform.OutputKeys.INDENT, "yes");
DOMSource source = new DOMSource(document);
StreamResult streamResult = new StreamResult(new File("ska.xml"));
transformer.transform(source, streamResult);
TransformerFactory factory = TransformerFactory.newInstance();
//StreamSource xsltStream = new StreamSource("ska.xslt");
StreamSource xsltStream = new StreamSource("ska_en.xslt");
Transformer transformer1 = factory.newTransformer(xsltStream);
StreamSource in = new StreamSource("ska.xml");
StreamResult out = new StreamResult("ska.html");
transformer1.transform(in, out);
System.out.println("Сгенерированный файл: ska.html");
}
}
import java.text.DateFormat;
import java.text.NumberFormat;
import java.util.*;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import javax.xml.transform.stream.StreamSource;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
public class Main {
public static void main(String[] args) throws Exception {
String lang = "en";
String country = "UK";
Locale l = new Locale(lang, country);
ResourceBundle r = ResourceBundle.getBundle("Bundle", l);
DateFormat fmt = DateFormat.getDateTimeInstance(DateFormat.MEDIUM, DateFormat.MEDIUM, l);
NumberFormat currFormat = NumberFormat.getInstance(l);
DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder();
Document document = documentBuilder.newDocument();
Element recipes = document.createElement("recipes");
document.appendChild(recipes);
//блюдо 1
Element recipe1 = document.createElement("recipe");
recipes.appendChild(recipe1);
recipe1.setAttribute("name", document.createTextNode("Блины").getData());
recipe1.setAttribute("preptime", currFormat.format(5));
recipe1.setAttribute("cooktime", currFormat.format(180));
recipe1.setAttribute("editDate", fmt.format(new Date(116, 0, 12, 12, 44)));
//ингредиенты
Element ingredient1 = document.createElement("ingredient");
recipe1.appendChild(ingredient1);
ingredient1.setAttribute("amount", currFormat.format(5000));
ingredient1.setAttribute("unit", r.getString("str1"));
ingredient1.appendChild(document.createTextNode("Мука блинная"));
Element ingredient2 = document.createElement("ingredient");
recipe1.appendChild(ingredient2);
ingredient2.setAttribute("amount", currFormat.format(5));
ingredient2.setAttribute("unit", r.getString("str2"));
ingredient2.appendChild(document.createTextNode("Яйцо куриное"));
Element ingredient3 = document.createElement("ingredient");
recipe1.appendChild(ingredient3);
ingredient3.setAttribute("amount", currFormat.format(1));
ingredient3.setAttribute("unit", r.getString("str3"));
ingredient3.appendChild(document.createTextNode("Молоко"));
Element ingredient4 = document.createElement("ingredient");
recipe1.appendChild(ingredient4);
ingredient4.setAttribute("amount", currFormat.format(1));
ingredient4.setAttribute("unit", r.getString("str4"));
ingredient4.appendChild(document.createTextNode("Соль"));
//Приборы
Element dishes1= document.createElement("dishes");
recipe1.appendChild(dishes1);
dishes1.appendChild(document.createTextNode("Миска"));
Element dishes2= document.createElement("dishes");
recipe1.appendChild(dishes2);
dishes2.appendChild(document.createTextNode("Сковородка"));
Element dishes3= document.createElement("dishes");
recipe1.appendChild(dishes3);
dishes3.appendChild(document.createTextNode("Миксер"));
//Пошаговая инструкция
Element instructions1= document.createElement("instructions");
recipe1.appendChild(instructions1);
Element step1= document.createElement("step");
instructions1.appendChild(step1);
step1.appendChild(document.createTextNode("Просеять муку и соль в миску."));
Element step2= document.createElement("step");
instructions1.appendChild(step2);
step2.appendChild(document.createTextNode("Добавить молока и перемешать."));
Element step3= document.createElement("step");
instructions1.appendChild(step3);
step3.appendChild(document.createTextNode("Перемешать, пока тесто не станет однородным."));
Element step4= document.createElement("step");
instructions1.appendChild(step4);
step4.appendChild(document.createTextNode("Разогреть немного масла на сковородке. Влить на сковородку 2-3 ст.ложки теста."));
Element step5= document.createElement("step");
instructions1.appendChild(step5);
step5.appendChild(document.createTextNode("Обжарить с одной стороны до золотистой корочки."));
Element step6= document.createElement("step");
instructions1.appendChild(step6);
step6.appendChild(document.createTextNode("Перевернуть и готовить с другой сторону около 30 секунд."));
TransformerFactory transformerFactory = TransformerFactory.newInstance();
Transformer transformer = transformerFactory.newTransformer();
transformer.setOutputProperty(javax.xml.transform.OutputKeys.INDENT, "yes");
DOMSource source = new DOMSource(document);
StreamResult streamResult = new StreamResult(new File("ska.xml"));
transformer.transform(source, streamResult);
TransformerFactory factory = TransformerFactory.newInstance();
//StreamSource xsltStream = new StreamSource("ska.xslt");
StreamSource xsltStream = new StreamSource("ska_en.xslt");
Transformer transformer1 = factory.newTransformer(xsltStream);
StreamSource in = new StreamSource("ska.xml");
StreamResult out = new StreamResult("ska.html");
transformer1.transform(in, out);
System.out.println("Сгенерированный файл: ska.html");
}
}