.


:




:

































 

 

 

 


StudentElement.addContent(addressElement);




// <student> <students>

Root.addContent(studentElement);

}

// XML-

Return new Document(root);

}

public static boolean saveDocument(String fileName,

Document doc) {

boolean complete = true;

XMLOutputter outputter = new XMLOutputter();

// XML-

try {

outputter.output(doc, new FileOutputStream(fileName));

} catch (FileNotFoundException e) {

e.printStackTrace();

complete = false;

} catch (IOException e) {

e.printStackTrace();

complete = false;

}

return complete;

}

}

/* # 11: : JDOMMainSaver.java*/

package chapt16.main;

import java.io.IOException;

import java.util.ArrayList;

import chapt16.entity.Student;

import chapt16.saver.dom.JDOMLogic;

 

public class JDOMMainSaver {

public static void main(String[] args) {

//

ArrayList<Student> students = new ArrayList<Student> ();

for (int j = 1; j < 3; j++) {

Student st = new Student();

st.setName("Petrov" + j);

st.setLogin("petr" + j);

st.setFaculty("mmf");

st.setTelephone("454556"+ j*3);

Student.Address adr = st.getAddress();

adr.setCity("Minsk");

adr.setCountry("BLR");

adr.setStreet("Gaja, " + j);

st.setAddress(adr);

students.add(st);

}

//

Document doc = JDOMLogic. create (students);

// XML-

if (JDOMLogic. saveDocument ("studentsnew.xml", doc))

System. out. println(" ");

Else

System. out. println(" ");

}

}

studentsnew.xml :

<?xml version="1.0" encoding="UTF-8"?>

<studentsnew>

<student login="petr1" phone="4545563">

<faculty>mmf</faculty>

<name>Petrov1</name>

<address>

<country>BLR</country>

<city>Minsk</city>

<street>Gaja, 1</street>

</address>

</student>

<student login="petr2" phone="4545566">

<faculty>mmf</faculty>

<name>Petrov2</name>

<address>

<country>BLR</country>

<city>Minsk</city>

<street>Gaja, 2</street>

</address>

</student>

</studentsnew>

JDOM, "if something doesn't work, fix it".

StAX

StAX (Streaming API for XML), pull-, JDK, Java SE 6. SAX XML, StAX , StAX- XML. , SAX, API XML-.

StAX XMLInputFactory, XMLStreamReader XMLOutputFactory, XMLStreamWriter, XML-. XML XMLStreamReader:

StringReader stringReader = new StringReader(xmlString);

XMLInputFactory inputFactory=XMLInputFactory.newInstance();

XMLStreamReader reader = inputFactory

CreateXMLStreamReader(stringReader);

XMLStreamReader Iterator, hasNext() next():

boolean hasNext() , ;

int next() XML, .

:

XMLStreamConstants.START_DOCUMENT

XMLStreamConstants.END_DOCUMENT

XMLStreamConstants.START_ELEMENT

XMLStreamConstants.END_ELEMENT

XMLStreamConstants.CHARACTERS

XMLStreamConstants.ATTRIBUTE

XMLStreamConstants.CDATA

XMLStreamConstants.NAMESPACE

XMLStreamConstants.COMMENT





:


: 2016-04-03; !; : 324 |


:

:

, .
==> ...

1501 - | 1408 -


© 2015-2024 lektsii.org - -

: 0.008 .