%26lt;root%26gt;
%26lt;tem engine="1.1" /%26gt;
%26lt;settings%26gt;
%26lt;installingUser value="john" /%26gt;
%26lt;installDir value="C:\\Program Files\\UGS\\Teamcenter\\Engineering\\200... /%26gt;
%26lt;tcserverActivationMode value="NORMAL" /%26gt;
%26lt;languageCode value="en" /%26gt;
%26lt;sourceDir value="W:\\SoftwareTC-SR1" /%26gt;
%26lt;portalConnectPort value="1575" /%26gt;
%26lt;countryCode value="US" /%26gt;
%26lt;licenseShown value="true" /%26gt;
%26lt;language value="English" /%26gt;
%26lt;version value="10.0.1.0" /%26gt;
%26lt;application value="tceng" /%26gt;
%26lt;/settings%26gt;.
in above xml i need to catch the value of John using java code..
please help
How to Parse xml file with java?
try jaxb
/*
* Creado el 11-04-2008
*
* TODO
*/
package cl.azurian.Clases;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;
import javax.xml.XMLConstants;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
import javax.xml.bind.Unmarshaller;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory...
import javax.xml.parsers.ParserConfigurationExc...
import javax.xml.transform.Source;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamSource;
import javax.xml.validation.Schema;
import javax.xml.validation.SchemaFactory;
import javax.xml.validation.Validator;
import org.w3c.dom.Document;
import org.xml.sax.SAXException;
import cl.azurian.Excepciones.NotValidXmlExcept...
public class ExampleMain {
public static Marshaller format;
/***
* Fichero a ser leido para Unmarshall
*/
public static String Fichero_Entrada = "C:/libros.xml";
/***
* Fichero que se generara por Marshaller
*/
public static String Fichero_Salida = "C:/ficheroMarshalling.xml";
/***
* Fichero de validación para el Xml
*/
public static String Fichero_Xsd = "C:/libros.xsd";
/***
*
* @param format
*
* @author jbovet
* @since 14-04-2008
*
*/
public static void setFormat(Marshaller format) {
ExampleMain.format = format;
}
public static void depura(String cadena) {
System.out.println(" " + cadena);
}
/***
* Clase principal
* @param args
* @throws NotValidXmlException
*
* @author jbovet
* @since 14-04-2008
*
*/
public static void main(String[] args) throws NotValidXmlException {
depura("------- INICIO -------");
try {
JAXBContext jxb = JAXBContext.newInstance("cl.azurian.Clas...
// creo obj Unmarshaller
Unmarshaller u = jxb.createUnmarshaller();
//verificamos si el Xml es valido
boolean isvalidXml = ValidaXml(Fichero_Entrada,Fichero_Xsd);
if (isvalidXml == true) {
InputStream inp = new FileInputStream(Fichero_Entrada);
if (inp.equals(null)) {
depura("No se encontro fichero");
}
// obj myfile de tipo Libro con modelo Unmarshall
Libros myfile = (Libros) u.unmarshal(inp);
// obtenemos los libros
List%26lt;Libro%26gt; miembros = myfile.getLibro();
System.out.println("Total Libros: " + miembros.size());
// Iteramos sobre la lista de libros
for (Libro list_libro : miembros) {
System.out.println("Autor: " + list_libro.getAutor()
+ " Titulo: " + list_libro.getTitulo()
+ " Id: " + list_libro.getId());
}
// agregamos nuevo libro
Libro nuevo = new Libro();
nuevo.setTitulo(" JAXB 2.0 ");
nuevo.setAutor("Wolfgang Schmiesing");
nuevo.setId(6);
// añadimos a la lista
miembros.add(nuevo);
// invocamos método Marshall para generar la salida del xml
Marshall(jxb, myfile);
System.out.println("Documento Válido ");
} else if (isvalidXml == false) {
throw new NotValidXmlException();
}
} catch (Exception e) {
System.out.println(e.getMessage());
}
}
/************************************...
* Método que realiza Marshalling ,genera la salida del fichero
*
* @param ctx Objeto JAXBContext
* @param file Objeto Libro
* @throws JAXBException
* @throws FileNotFoundException
*
* @author jbovet
* @since 14-04-2008
*
*/
public static void Marshall(JAXBContext ctx, Libros file)
throws JAXBException, FileNotFoundException {
format = ctx.createMarshaller();
format.setProperty(Marshaller.JAXB_F... Boolean.TRUE);
System.out.println("\n");
System.out.println("/************* SALIDA XML ********************/\n");
System.out.flush();
// print Marshall
format.marshal(file, System.out);
// Configuramos el fichero de salida
format.setProperty(Marshaller.JAXB_E... "UTF-8");
format.setProperty(Marshaller.JAXB_S...
FileOutputStream salida_file = new FileOutputStream(Fichero_Salida);
// Genera la salida del fichero
format.marshal(file, salida_file);
}
/***
* Método que valida archivo xml contra un xsd
* @param fileXml : Archivo Xml
* @param schemafile : Xsd de validación
* @return boolean : true si es un Xml válido, false, si no lo es.
* @throws ParserConfigurationException
* @throws SAXException
* @throws IOException
*
* @author jbovet
* @since 15-04-2008
*
*/
public static boolean ValidaXml(String fileXml, String schemafile) throws ParserConfigurationException,
SAXException, IOException {
// xml - DOM
DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance();
builderFactory.setNamespaceAware(tru...
DocumentBuilder psr = builderFactory.newDocumentBuilder();
Document doc = psr.parse(new File(fileXml));
// http://www.w3.org/2001/XMLSchema
SchemaFactory factory = SchemaFactory.newInstance(XMLConstants.W...
Source filexsd = new StreamSource(new File(schemafile));
Schema schema = factory.newSchema(filexsd);
Validator valida = schema.newValidator();
// Intenta validar DOM
try {
valida.validate(new DOMSource(doc));
return true;
} catch (SAXException saxe) {
System.out.println("- Documento Inválido - ");
System.out.print(saxe.getMessage())...
return false;
}
}
}
flash cards
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment