Showing posts with label XML. Show all posts
Showing posts with label XML. Show all posts

13 August, 2009

Bind XML to Java classes

Just follow this example:


cbr_daily.xml
<ValCurs Date="30/05/2009" name="Foreign Currency Market">
<Valute ID="R01035">
<NumCode>826</NumCode>
<CharCode>GBP</CharCode>
<Nominal>1</Nominal>
<Name>Фунт стерлингов Соединенного королевства</Name>
<Value>49,7887</Value>
</Valute>
<Valute ID="R01235">
<NumCode>840</NumCode>
<CharCode>USD</CharCode>
<Nominal>1</Nominal>
<Name>Доллар США</Name>
<Value>30,9843</Value>
</Valute>
<Valute ID="R01239">
<NumCode>978</NumCode>
<CharCode>EUR</CharCode>
<Nominal>1</Nominal>
<Name>Евро</Name>
<Value>43,3780</Value>
</Valute>
</ValCurs>



ValCursType.java
package xqx;

import java.util.List;
import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlType;

@XmlRootElement(name = "ValCurs")
@XmlType(name = "ValCursType")
public class ValCursType {

@XmlElement(name = "Valute")
public List<ValuteType> valuteType;

@XmlAttribute(name = "Date")
public String date;

@XmlAttribute
public String name;
}




ValuteType.java
package xqx;

import javax.xml.bind.annotation.*;

@XmlRootElement(name = "Valute")
@XmlType(name = "ValuteType")
public class ValuteType {
@XmlElement(name = "NumCode")
public String numCode;

@XmlElement(name = "CharCode")
public String charCode;

@XmlElement(name = "Nominal")
public int nominal;

@XmlElement(name = "Name")
public String name;

@XmlElement(name = "Value")
public String value;

@XmlAttribute(name = "ID")
public String id;
}




CbrCurrencyTask.java
package xqx;

import ...;

public class CbrCurrencyTask {

...
//stream - For example FileInputStream created from the "cbr_daily.xml" file
protected ValCursType parse(InputStream stream) throws JAXBException {
JAXBContext jaxbContext = JAXBContext.newInstance(ValCursType.class);
Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();

ValCursType valCursType = (ValCursType) unmarshaller.unmarshal(stream);
return valCursType;
}
...

}


Simple! Isn't it?

17 October, 2008

emc

One more mine project: emc - remote file navigator.

It developed on:
  • Client side - Flex
  • Server side - PHP
  • RPC container - XML

This is just beta version.

It is possible to release complete version (add new functionality or implement another Server side, for example). And I'm looking for the people, who want to use this in their commercial projects.

18 September, 2008

Namespaces for custom Flex components

Flex (MXML) allow to declare custom components via xmlns declaration.
Like this:
<mx:Box xmlns:mx="http://www.adobe.com/2006/mxml"
xmlns:emc="emc.components.navigator.*">

This way is useful when amount of custom components not too much and all components are located in the one or two packages.

But what about real namespaces like in Flex itself ("xmlns:mx="http://www.adobe.com/2006/mxml"); for huge amount of components?

It is possible to consolidate declaration of all components in the single file.
Like this:

manifest.xml
<?xml version="1.0"?>

<componentPackage>
<component id="Navigator" class="emc.components.navigator.Navigator"/>
<component id="Editor" class="emc.components.editor.Editor"/>
<component id="Console" class="emc.components.console.Console"/>
<component id="SmartTruncableLabel" class="emc.components.SmartTruncableLabel"/>
<component id="AutoScrollableTextArea" class="emc.components.AutoScrollableTextArea"/>
</componentPackage>
put this file into the project sources directory and add this argument to the MXML compiler:
-namespace=http://xantorohara.110mb.com/emc,sources/manifest.xml
and then use this components in the MXML files via this declaration:
<mx:Box xmlns:mx="http://www.adobe.com/2006/mxml"
xmlns:emc="http://xantorohara.110mb.com/emc">
and insert component into your application:
...
<emc:SmartTruncableLabel id="titleLabel" styleName="filePanelTitleLabel" width="100%" />
...

How do you like it?

Simple URL rewrite rule for Apache

These strings in the ".htaccess" file redirect HTTP query from "worker.xml" file to "worker.php":
RewriteEngine on
RewriteBase /worker
RewriteRule worker.xml worker.php

So, it is possible to create XML (or another) facade around PHP (or another) server-side implementation.

16 January, 2008

Simple RTF to XML converter

RTFEditorKit (javax.swing.text.rtf.RTFEditorKit) from Sun Java API - special class for operations with RTF (Rich Text Format) documents.

I've created java sample that converts RTF document to XML.

This is the source of this converter:



Rtf2XML.java import javax.swing.text.AbstractDocument.BranchElement; import javax.swing.text.DefaultStyledDocument; import javax.swing.text.BadLocationException; import javax.swing.text.rtf.RTFEditorKit; import javax.xml.parsers.DocumentBuilderFactory; import javax.xml.transform.Transformer; import javax.xml.transform.TransformerFactory; import javax.xml.transform.dom.DOMSource; import javax.xml.transform.stream.StreamResult; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.UnsupportedEncodingException; public class Rtf2XML { private DefaultStyledDocument rtfSource; private org.w3c.dom.Document xmlTarget; private org.w3c.dom.Element xmlRoot; private void expandElement(javax.swing.text.Element rtfElement) { for (int i = 0; i < rtfElement.getElementCount(); i++) { javax.swing.text.Element rtfNextElement = rtfElement.getElement(i); if (rtfNextElement.isLeaf()) { try { addElement(rtfNextElement); } catch (Exception e) { e.printStackTrace(); } } else { expandElement(rtfNextElement); } } } private void addElement(javax.swing.text.Element rtfElement) throws UnsupportedEncodingException, BadLocationException { String style = new String(rtfSource.getLogicalStyle(rtfElement.getStartOffset()) .getName().getBytes("ISO-8859-1")); String text = new String(rtfSource.getText(rtfElement.getStartOffset(), rtfElement.getEndOffset() - rtfElement.getStartOffset()) .getBytes("ISO-8859-1")); org.w3c.dom.Element node = xmlTarget.createElement("p"); node.appendChild(xmlTarget.createTextNode(text)); node.setAttribute("style", style); xmlRoot.appendChild(node); } public void convert(String sourceFileName) throws Exception { rtfSource = new DefaultStyledDocument(); RTFEditorKit kit = new RTFEditorKit(); kit.read(new FileInputStream(sourceFileName), rtfSource, 0); xmlTarget = DocumentBuilderFactory.newInstance() .newDocumentBuilder().newDocument(); BranchElement rtfRoot = (BranchElement) rtfSource.getDefaultRootElement(); xmlRoot = xmlTarget.createElement("data"); expandElement(rtfRoot); xmlTarget.appendChild(xmlRoot); Transformer t = TransformerFactory.newInstance().newTransformer(); t.transform(new DOMSource(xmlTarget), new StreamResult(new FileOutputStream(sourceFileName + ".xml"))); } public static void main(String[] args) { if (args.length != 1) { System.err.println("Usage: *.rtf"); return; } try { new Rtf2XML().convert(args[0]); } catch (Exception e) { e.printStackTrace(); } } }


But RTFEditorKit isn't so powerful and friendly as I want.
I think, iText will be better for operations with RTF (and other document formats).

It's the good and free decision.

17 July, 2007

HowTo post html code into blog

Sometimes bloggers need to publish some text, which not displayed correctly into their blog.

For example, you can't post this html directly into your blog:

<html>
<head>
<title>XLam</title>
</head>
<body>
<script language='javascript' src='xqx.web.xlam.XLam.nocache.js'></script>
<div id='uid'></div>
</body>
</html>

but you can convert it into this format:

&lt;html&gt;
&lt;head&gt;
&lt;title&gt;XLam&lt;/title&gt;
&lt;/head&gt;
&lt;body&gt;
&lt;script language='javascript' src='xqx.web.xlam.XLam.nocache.js'&gt;&lt;/script&gt;
&lt;div id='uid'&gt;&lt;/div&gt;
&lt;/body&gt;
&lt;/html&gt;

and then successfully publish it.

For this purpose, bloggers may use this script:

#!/bin/bash

#This simple shell script replaces symbols in the given xml/html file and writes it to file file.blog
#Symbols to replace:
# '<' with &lt;
# '>' with &gt;
# '&' with &amp;
#
#Usage: html2blog.sh file

sed -e 's/\x26/\&amp;/g;s/\x3c/\&lt;/g;s/\x3e/\&gt;/g' $1 >$1.blog


It works on Linux/Unix platforms or under Cygwin environment.