Buongiorno a tutti!
Devo implementare una classe che mi permetta di trattare degli xml il cui schema è il seguente
ed in particolare gli elementi title e body devono essere codificati in iso-8859-1 (anzichè in utf-8), allora dopo delle ricerche in rete sono arrivato a questa conclusione:codice:<?xml version="1.0" encoding="UTF-8"?> <xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:tns="http://data.webwrite/message/" xmlns:uns="http://data.webwrite/account/" targetNamespace="http://data.webwrite/message/" elementFormDefault="qualified"> <xsd:import namespace="http://data.webwrite/account/" schemaLocation="account.xsd" /> <xsd:simpleType name="dateType"> <xsd:restriction base="xsd:string"> <xsd:pattern value="\d+[/]\d+[/]\d{4}" /> </xsd:restriction> </xsd:simpleType> <xsd:simpleType name="timeType"> <xsd:restriction base="xsd:string"> <xsd:pattern value="\d+[:]\d+" /> </xsd:restriction> </xsd:simpleType> <xsd:simpleType name="titleType"> <xsd:restriction base="xsd:string"> <xsd:minLength value="1" /> <xsd:maxLength value="250" /> </xsd:restriction> </xsd:simpleType> <xsd:simpleType name="bodyType"> <xsd:restriction base="xsd:string"> <xsd:minLength value="1" /> <xsd:maxLength value="1024" /> </xsd:restriction> </xsd:simpleType> <xsd:complexType name="messageType"> <xsd:sequence> <xsd:element name="date" type="tns:dateType" /> <xsd:element name="time" type="tns:timeType" /> <xsd:element name="title" type="tns:titleType" /> <xsd:element name="body" type="tns:bodyType" /> </xsd:sequence> <xsd:attribute name="user" type="uns:usernameType" /> </xsd:complexType> <xsd:element name="message" type="tns:messageType" /> </xsd:schema>
Ora la codifica è corretta in quanto l'xml che mi viene restituito è il seguente:codice:DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); Document document = factory.newDocumentBuilder().newDocument(); MessageType message = new MessageType(); message.setTitle("Ciao è"); message.setBody("Come stai é"); message.setUser("io"); Element messageElem = document.createElementNS(Constants.MESSAGE_NS, "message"); Element date = document.createElementNS(Constants.MESSAGE_NS, "date"); date.setTextContent(message.getDate()); messageElem.appendChild(date); Element time = document.createElementNS(Constants.MESSAGE_NS, "time"); time.setTextContent(message.getTime()); messageElem.appendChild(time); Element title = document.createElementNS(Constants.MESSAGE_NS, "title"); title.setTextContent(message.getTitle()); messageElem.appendChild(title); Element body = document.createElementNS(Constants.MESSAGE_NS, "body"); body.setTextContent(message.getBody()); messageElem.appendChild(body); messageElem.setAttribute("user", message.getUser()); document.appendChild(messageElem); OutputFormat format = new OutputFormat(document); format.setIndenting(true); format.setIndent(2); format.setEncoding("iso-8859-1"); StringWriter sw = new StringWriter(); XMLSerializer serializer = new XMLSerializer(sw, format); serializer.serialize(document); String xml = sw.toString(); System.out.println(xml);
ma il namespace non viene specificato: message dovrebbe apparire come seguecodice:<?xml version="1.0" encoding="iso-8859-1"?> <message user="io"> <date>2/6/2010</date> <time>14:48</time> <title>Ciao è</title> <body>Come stai é</body> </message>
Qualcuno mi sa dire come fare? Grazie infinite!codice:<message xmlns="http://data.webwrite/message/" user="io">

Rispondi quotando