NotesWhat is notes.io?

Notes brand slogan

Notes - notes.io

Write a note in this area. It's really easy to share with others. Click here ...//Taken from various sources, including: http://labe.felk.cvut.cz/~xfaigl/mep/xml/java-xml.htm

import java.net.*;
import java.io.*;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerConfigurationException;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Text;

public class urlreader1 {
private String target = "D:/example2.xml";
public static void main(String[] args) throws Exception {
URL yahoo = new URL("http://www.computing.northampton.ac.uk/~scott/csy3025/sample1.html");
DocumentBuilderFactory dbfac = DocumentBuilderFactory.newInstance();
DocumentBuilder docBuilder = dbfac.newDocumentBuilder();
Document doc = docBuilder.newDocument();
BufferedReader in = new BufferedReader(
new InputStreamReader(
yahoo.openStream()));

String inputLine;
String first="a";
String second="a";
String third="a";
String forth="a";
String fifth="a";
String sixth="a";



while ((inputLine = in.readLine()) != null)//reads in the html page line by line
{
String inputLine2=inputLine.replaceAll(""", "");//removes speechmarks from text
String[] parts = inputLine2.split("<span itemprop=");//splits into sections based on it is after <span class="
for (int loop=0;loop<parts.length;loop++)
{
System.out.println(parts[loop]);
if (parts[loop].contains("name>"))
{
first=parts[loop].substring(13, (parts[loop].length())-16);
}
if (parts[loop].contains("jobTitle>"))
{
second=parts[loop].substring(9, (parts[loop].length())-7);
}
if (parts[loop].contains("worksFor>"))
{
third=parts[loop].substring(9, (parts[loop].length())-7);
}
if (parts[loop].contains("address>"))
{
forth=parts[loop].substring(8, (parts[loop].length())-7);
}
if (parts[loop].contains("postalCode>"))
{
fifth=parts[loop].substring(11, (parts[loop].length())-7);
}
if (parts[loop].contains("email>"))
{
sixth=parts[loop].substring(6, (parts[loop].length())-7);
}
}
}

in.close();
System.out.println("Extracted first name "+first);
System.out.println("Extracted surname "+second);
try {
/////////////////////////////
//Creating an empty XML Document




//Creating the XML tree

Element root = doc.createElement("address_1");
doc.appendChild(root);



//create child element, add an attribute, and add to root
Element first1 = doc.createElement("Name");
root.appendChild(first1);

//add a text element to the child
Text text = doc.createTextNode(first);
first1.appendChild(text);

Element first2 = doc.createElement("Job_title");
root.appendChild(first2);

//add a text element to the child
Text text2 = doc.createTextNode(second);
first2.appendChild(text2);
//Output the XML

Element first3 = doc.createElement("Employer");
root.appendChild(first3);

//add a text element to the child
Text text3 = doc.createTextNode(third);
first3.appendChild(text3);
//Output the XML

Element first4 = doc.createElement("Address");
root.appendChild(first4);

//add a text element to the child
Text text4 = doc.createTextNode(forth);
first4.appendChild(text4);
//Output the XML
Element first5 = doc.createElement("PostalCode");
root.appendChild(first5);

//add a text element to the child
Text text5 = doc.createTextNode(fifth);
first5.appendChild(text5);
//Output the XML

Element first6 = doc.createElement("Email");
root.appendChild(first6);

//add a text element to the child
Text text6 = doc.createTextNode(sixth);
first6.appendChild(text6);
//Output the XML

//set up a transformer
TransformerFactory transfac = TransformerFactory.newInstance();
Transformer trans = transfac.newTransformer();
trans.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
trans.setOutputProperty(OutputKeys.INDENT, "yes");

//create string from xml tree
StringWriter sw = new StringWriter();
StreamResult result = new StreamResult(sw);
DOMSource source = new DOMSource(doc);
trans.transform(source, result);
String xmlString = sw.toString();

//print xml
System.out.println("Here's the xml:nn" + xmlString);

} catch (Exception e) {
System.out.println(e);

}
saveXMLDocument("D://example2.xml", doc);
}
public static boolean saveXMLDocument(String fileName, Document doc) {
System.out.println("Saving XML file... " + fileName);
// open output stream where XML Document will be saved
File xmlOutputFile = new File(fileName);
FileOutputStream fos;
Transformer transformer;
try {
fos = new FileOutputStream(xmlOutputFile);
}
catch (FileNotFoundException e) {
System.out.println("Error occured: " + e.getMessage());
return false;
}
// Use a Transformer for output
TransformerFactory transformerFactory = TransformerFactory.newInstance();
try {
transformer = transformerFactory.newTransformer();
}
catch (TransformerConfigurationException e) {
System.out.println("Transformer configuration error: " + e.getMessage());
return false;
}
DOMSource source = new DOMSource(doc);
StreamResult result = new StreamResult(fos);
// transform source into result will do save
try {
transformer.transform(source, result);
}
catch (TransformerException e) {
System.out.println("Error transform: " + e.getMessage());
}
System.out.println("XML file saved.");
return true;
}
}
     
 
what is notes.io
 

Notes.io is a web-based application for taking notes. You can take your notes and share with others people. If you like taking long notes, notes.io is designed for you. To date, over 8,000,000,000 notes created and continuing...

With notes.io;

  • * You can take a note from anywhere and any device with internet connection.
  • * You can share the notes in social platforms (YouTube, Facebook, Twitter, instagram etc.).
  • * You can quickly share your contents without website, blog and e-mail.
  • * You don't need to create any Account to share a note. As you wish you can use quick, easy and best shortened notes with sms, websites, e-mail, or messaging services (WhatsApp, iMessage, Telegram, Signal).
  • * Notes.io has fabulous infrastructure design for a short link and allows you to share the note as an easy and understandable link.

Fast: Notes.io is built for speed and performance. You can take a notes quickly and browse your archive.

Easy: Notes.io doesn’t require installation. Just write and share note!

Short: Notes.io’s url just 8 character. You’ll get shorten link of your note when you want to share. (Ex: notes.io/q )

Free: Notes.io works for 12 years and has been free since the day it was started.


You immediately create your first note and start sharing with the ones you wish. If you want to contact us, you can use the following communication channels;


Email: [email protected]

Twitter: http://twitter.com/notesio

Instagram: http://instagram.com/notes.io

Facebook: http://facebook.com/notesio



Regards;
Notes.io Team

     
 
Shortened Note Link
 
 
Looding Image
 
     
 
Long File
 
 

For written notes was greater than 18KB Unable to shorten.

To be smaller than 18KB, please organize your notes, or sign in.