bon j’ai ca comme erreur et je ne pige pas trop le xml est valide pourtant :
[codebox]
java.net.MalformedURLException: no protocol: <?xml version="1.0" encoding="UTF-8"?>Seagull367434Robin367525Crow367628
at java.net.URL.(Unknown Source)
at java.net.URL.(Unknown Source)
at java.net.URL.(Unknown Source)
at com.sun.org.apache.xerces.internal.impl.XMLEntityManager.setupCurrentEntity(Unkn
own Source)
at com.sun.org.apache.xerces.internal.impl.XMLVersionDetector.determineDocVersion(U
nknown Source)
at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(Unknown Source)
at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(Unknown Source)
at com.sun.org.apache.xerces.internal.parsers.XMLParser.parse(Unknown Source)No of Employees ‘0’.
at com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser.parse(Unknown Source)
at javax.xml.parsers.SAXParser.parse(Unknown Source)
at javax.xml.parsers.SAXParser.parse(Unknown Source)
at xmlparser.XmlParser.parseDocument(XmlParser.java:47)
at xmlparser.XmlParser.runExample(XmlParser.java:31)
at xmlparser.XmlParser.main(XmlParser.java:107)
[/codebox]
et mon source c’est
[codebox]
package xmlparser;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;
public class XmlParser extends DefaultHandler{
List myEmpls;
private String tempVal;
//to maintain context
private Employee tempEmp;
public XmlParser(){
myEmpls = new ArrayList();
}
public void runExample() {
parseDocument();
printData();
}
private void parseDocument() {
//get a factory
SAXParserFactory spf = SAXParserFactory.newInstance();
try {
//get a new instance of parser
SAXParser sp = spf.newSAXParser();
//parse the file and also register this class for call backs
String xml = "<?xml version=\"1.0\" encoding=\"UTF-8\"?><Personnel><Employee type=\"permanent\"><Name>Seagull</Name><Id>3674</Id><Age>34</Age></Employee><Employee type=\"contract\"><Name>Robin</Name><Id>3675</Id><Age>25</Age></Employee><Employee type=\"permanent\"><Name>Crow</Name><Id>3676</Id><Age>28</Age></Employee></Personnel>";
sp.parse(xml, this);
}catch(SAXException se) {
se.printStackTrace();
}catch(ParserConfigurationException pce) {
pce.printStackTrace();
}catch (IOException ie) {
ie.printStackTrace();
}
}
/**
* Iterate through the list and print
* the contents
*/
private void printData(){
System.out.println("No of Employees '" + myEmpls.size() + "'.");
Iterator it = myEmpls.iterator();
while(it.hasNext()) {
System.out.println(it.next().toString());
}
}
//Event Handlers
public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
//reset
tempVal = "";
if(qName.equalsIgnoreCase("Employee")) {
//create a new instance of employee
tempEmp = new Employee();
tempEmp.setType(attributes.getValue("type"));
}
}
public void characters(char[] ch, int start, int length) throws SAXException {
tempVal = new String(ch,start,length);
}
public void endElement(String uri, String localName, String qName) throws SAXException {
if(qName.equalsIgnoreCase("Employee")) {
//add it to the list
myEmpls.add(tempEmp);
}else if (qName.equalsIgnoreCase("Name")) {
tempEmp.setName(tempVal);
}else if (qName.equalsIgnoreCase("Id")) {
tempEmp.setId(Integer.parseInt(tempVal));
}else if (qName.equalsIgnoreCase("Age")) {
tempEmp.setAge(Integer.parseInt(tempVal));
}
}
public static void main(String[] args){
XmlParser spe = new XmlParser();
spe.runExample();
}
}
[/codebox]
je viens d’essayer en mettant ca :
[codebox]
String xml = “<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n\n<Employee type=“permanent”>\nSeagull\n3674\n34\n\n<Employee type=“contract”>\nRobin\n3675\n25\n\n<Employee type=“permanent”>\nCrow\n3676\n28\n\n”;
[/codebox]
ca bug toujours de la meme maniere