protected void toXml(Record record) throws SAXException { if (!MarcFactory.newInstance().validateRecord(record)) { throw new MarcException("Marc record didn't validate"); } char temp[]; AttributesImpl atts = new AttributesImpl(); if (indent) handler.ignorableWhitespace("\n ".toCharArray(), 0, 3); handler.startElement(Constants.MARCXML_NS_URI, RECORD, RECORD, atts); if (indent) handler.ignorableWhitespace("\n ".toCharArray(), 0, 5); handler.startElement(Constants.MARCXML_NS_URI, LEADER, LEADER, atts); Leader leader = record.getLeader(); temp = leader.toString().toCharArray(); handler.characters(temp, 0, temp.length); handler.endElement(Constants.MARCXML_NS_URI, LEADER, LEADER); for (ControlField field : record.getControlFields()) { atts = new AttributesImpl(); atts.addAttribute("", "tag", "tag", "CDATA", field.getTag()); if (indent) handler.ignorableWhitespace("\n ".toCharArray(), 0, 5); handler.startElement(Constants.MARCXML_NS_URI, CONTROL_FIELD, CONTROL_FIELD, atts); temp = getDataElement(field.getData()); handler.characters(temp, 0, temp.length); handler.endElement(Constants.MARCXML_NS_URI, CONTROL_FIELD, CONTROL_FIELD); } for (DataField field : record.getDataFields()) { atts = new AttributesImpl(); atts.addAttribute("", "tag", "tag", "CDATA", field.getTag()); atts.addAttribute("", "ind1", "ind1", "CDATA", String.valueOf(field.getIndicator1())); atts.addAttribute("", "ind2", "ind2", "CDATA", String.valueOf(field.getIndicator2())); if (indent) handler.ignorableWhitespace("\n ".toCharArray(), 0, 5); handler.startElement(Constants.MARCXML_NS_URI, DATA_FIELD, DATA_FIELD, atts); for (Subfield subfield : field.getSubfields()) { atts = new AttributesImpl(); atts.addAttribute("", "code", "code", "CDATA", String.valueOf(subfield.getCode())); if (indent) handler.ignorableWhitespace("\n ".toCharArray(), 0, 7); handler.startElement(Constants.MARCXML_NS_URI, SUBFIELD, SUBFIELD, atts); temp = getDataElement(subfield.getData()); handler.characters(temp, 0, temp.length); handler.endElement(Constants.MARCXML_NS_URI, SUBFIELD, SUBFIELD); } if (indent) handler.ignorableWhitespace("\n ".toCharArray(), 0, 5); handler.endElement(Constants.MARCXML_NS_URI, DATA_FIELD, DATA_FIELD); } if (indent) handler.ignorableWhitespace("\n ".toCharArray(), 0, 3); handler.endElement(Constants.MARCXML_NS_URI, RECORD, RECORD); }
/** * Save the specified object. * * @param object The object to save. * @param hd The XML object. */ public void save(final EncogPersistedObject object, final TransformerHandler hd) { try { final BasicNeuralDataSet set = (BasicNeuralDataSet) object; final AttributesImpl atts = EncogPersistedCollection.createAttributes(object); hd.startElement("", "", "BasicNeuralDataSet", atts); atts.clear(); for (final NeuralDataPair pair : set) { hd.startElement("", "", this.pairXML, atts); hd.startElement("", "", this.inputXML, atts); for (int i = 0; i < pair.getInput().size(); i++) { hd.startElement("", "", this.valueXML, atts); final String data = "" + pair.getInput().getData(i); hd.characters(data.toCharArray(), 0, data.length()); hd.endElement("", "", this.valueXML); } hd.endElement("", "", this.inputXML); hd.startElement("", "", this.idealXML, atts); for (int i = 0; i < pair.getIdeal().size(); i++) { hd.startElement("", "", this.valueXML, atts); final String data = "" + pair.getIdeal().getData(i); hd.characters(data.toCharArray(), 0, data.length()); hd.endElement("", "", this.valueXML); } hd.endElement("", "", this.idealXML); hd.endElement("", "", this.pairXML); } hd.endElement("", "", "BasicNeuralDataSet"); } catch (final SAXException e) { throw new NeuralNetworkError(e); } }
@Override public String serialize(List<Book> books) throws Exception { SAXTransformerFactory factory = (SAXTransformerFactory) TransformerFactory.newInstance(); // 取得SAXTransformerFactory实例 TransformerHandler handler = factory.newTransformerHandler(); // 从factory获取TransformerHandler实例 Transformer transformer = handler.getTransformer(); // 从handler获取Transformer实例 transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8"); // 设置输出采用的编码方式 transformer.setOutputProperty(OutputKeys.INDENT, "yes"); // 是否自动添加额外的空白 transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "no"); // 是否忽略XML声明 StringWriter writer = new StringWriter(); Result result = new StreamResult(writer); handler.setResult(result); String uri = ""; // 代表命名空间的URI 当URI无值时 须置为空字符串 String localName = ""; // 命名空间的本地名称(不包含前缀) 当没有进行命名空间处理时 须置为空字符串 handler.startDocument(); handler.startElement(uri, localName, "books", null); AttributesImpl attrs = new AttributesImpl(); // 负责存放元素的属性信息 char[] ch = null; for (Book book : books) { attrs.clear(); // 清空属性列表 attrs.addAttribute( uri, localName, "id", "string", String.valueOf(book.getId())); // 添加一个名为id的属性(type影响不大,这里设为string) handler.startElement(uri, localName, "book", attrs); // 开始一个book元素 // 关联上面设定的id属性 handler.startElement(uri, localName, "name", null); // 开始一个name元素 // 没有属性 ch = String.valueOf(book.getName()).toCharArray(); handler.characters(ch, 0, ch.length); // 设置name元素的文本节点 handler.endElement(uri, localName, "name"); handler.startElement(uri, localName, "price", null); // 开始一个price元素 // 没有属性 ch = String.valueOf(book.getPrice()).toCharArray(); handler.characters(ch, 0, ch.length); // 设置price元素的文本节点 handler.endElement(uri, localName, "price"); handler.endElement(uri, localName, "book"); } handler.endElement(uri, localName, "books"); handler.endDocument(); return writer.toString(); }
private static void serializeEntry(TransformerHandler hd, Entry entry) throws SAXException { AttributesImpl entryAttributes = new AttributesImpl(); for (Iterator<String> it = entry.getAttributes().iterator(); it.hasNext(); ) { String key = it.next(); entryAttributes.addAttribute("", "", key, "", entry.getAttributes().getValue(key)); } hd.startElement("", "", ENTRY_ELEMENT, entryAttributes); StringList tokens = entry.getTokens(); for (Iterator<String> it = tokens.iterator(); it.hasNext(); ) { hd.startElement("", "", TOKEN_ELEMENT, new AttributesImpl()); String token = it.next(); hd.characters(token.toCharArray(), 0, token.length()); hd.endElement("", "", TOKEN_ELEMENT); } hd.endElement("", "", ENTRY_ELEMENT); }
private void addElement(final String element, final String string) throws SAXException { final AttributesImpl noAttributes = new AttributesImpl(); transformerHandler.startElement("", "", element, noAttributes); if (string != null) { transformerHandler.characters(string.toCharArray(), 0, string.length()); } transformerHandler.endElement("", "", element); }