Zobrazují se příspěvky se štítkemxml. Zobrazit všechny příspěvky
Zobrazují se příspěvky se štítkemxml. Zobrazit všechny příspěvky

neděle 7. července 2013

Convert XML to CSV example

Input CSV

 <?xml version="1.0" encoding="utf-8"?>  
 <detail id="1243">  
  <nazev>Praktický lékař pro dospělé</nazev>  
  <jmeno>MUDr. Nomen Omen</jmeno>  
  <adresa>Street 1/234, 18000 City 8</adresa>  
  <telefon>89879076</telefon>  
  <fax/>  
  <ico>8908078</ico>  
  <poradove-cislo>000</poradove-cislo>  
  <poradove-cislo-det-prac>000</poradove-cislo-det-prac>  
  <nazev-zrizovatele>Fyzická osoba</nazev-zrizovatele>  
  <druh-zz>320 Samost.ordinace prakt.lék.pro dospělé</druh-zz>  
  <email>nomen-omen@mailer.ru</email>  
  <www/>  
 </detail>  

XSLT template file - takes one parametr output-header - when true, header is output first, output is Tab separated, no escaping nor quoting is done

 <xsl:stylesheet version="1.0"  
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform">  
  <xsl:output method="text" omit-xml-declaration="yes" indent="no"/>  
  <xsl:param name="output-header" value="false"/>  
  <xsl:template match="/">  
   <xsl:if test="$output-header='true'">  
    <xsl:value-of select="concat('id','&#x09;')"/>  
    <xsl:value-of select="concat('type','&#x09;')"/>  
    <xsl:value-of select="concat('nazev','&#x09;')"/>  
    <xsl:value-of select="concat('jmeno','&#x09;')"/>  
    <xsl:value-of select="concat('adresa','&#x09;')"/>  
    <xsl:value-of select="concat('telefon','&#x09;')"/>  
    <xsl:value-of select="concat('fax','&#x09;')"/>  
    <xsl:value-of select="concat('ico','&#x09;')"/>  
    <xsl:value-of select="concat('poradove-cislo','&#x09;')"/>  
    <xsl:value-of select="concat('poradove-cislo-det-prac','&#x09;')"/>  
    <xsl:value-of select="concat('nazev-zrizovatele','&#x09;')"/>  
    <xsl:value-of select="concat('druh-zz','&#x09;')"/>  
    <xsl:value-of select="concat('email','&#x09;')"/>  
    <xsl:value-of select="concat('www','')"/>  
    <xsl:text>  
 </xsl:text>  
   </xsl:if>  
   <xsl:for-each select="//detail">  
    <xsl:value-of select="concat(@id,'&#x09;')"/>  
    <xsl:value-of select="concat('detail','&#x09;')"/>  
    <xsl:value-of select="concat(nazev,'&#x09;')"/>  
    <xsl:value-of select="concat(jmeno,'&#x09;')"/>  
    <xsl:value-of select="concat(adresa,'&#x09;')"/>  
    <xsl:value-of select="concat(telefon,'&#x09;')"/>  
    <xsl:value-of select="concat(fax,'&#x09;')"/>  
    <xsl:value-of select="concat(ico,'&#x09;')"/>  
    <xsl:value-of select="concat(poradove-cislo,'&#x09;')"/>  
    <xsl:value-of select="concat(poradove-cislo-det-prac,'&#x09;')"/>  
    <xsl:value-of select="concat(nazev-zrizovatele,'&#x09;')"/>  
    <xsl:value-of select="concat(druh-zz,'&#x09;')"/>  
    <xsl:value-of select="concat(email,'&#x09;')"/>  
    <xsl:value-of select="concat(www,'')"/>  
    <xsl:text>  
 </xsl:text>  
   </xsl:for-each></xsl:template>  
 </xsl:stylesheet>  

Customization: use your own element names, template matching rule and whatever to process more complex XMLs. Keep the structure (spaces, line ends) of the  tailing <xsl:text> to get proper line ends at the right places.

středa 11. listopadu 2009

... sleepless night full of programming and debugging

I didn't sleep last night. There is a nasty bug in the Java XML parser. Under certain circumstances (inherited default namespace with together with attributes present) the parser presents a rich imagination. In this case an element namespace properties are wrong. No namespace URI is assigned to the element and the null namespace is attached.
As a result when serialized extra namespace definition appears xmlns="" and as a result the element is not bound to any namespace. A colleague of mine performed an excellent analysis of the problem.

Here is a result of the sleepless night (traverseDom method walk the tree recursively calling processNode on each node:

traverseDom(doc.getDocumentElement(),
new NodeProcessor() {
public void processNode(Node n) {
//TODO watch this in case the java xml parser will get better in future versions
//TODO watch this for general purpose XML signature processing where such elements may be legal
//fix parser error - we can afford this as no element is unqualified
// in case there is a unqualified element with some attributes with default null namespace
// it is concidered as an error produced by buggy parser
if (n.getNodeType()==n.ELEMENT_NODE &&
n.getAttributes().getLength()!=0 &&
n.isDefaultNamespace(null) &&
n.getNamespaceURI()==null){
String prfx=n.lookupPrefix(n.getParentNode().getNamespaceURI())
n.getOwnerDocument().renameNode(n,
n.getParentNode().getNamespaceURI(), prfx+":"+n.getLocalName());
}
}
}, true);

pátek 11. září 2009

.. Apache XML Security v 1.4.3 and Java 6

It is not easy to get this combination up and running. Java 6 contains XML Signature implementation (JCP105). The implementaion is based on XML Signature Syntax and Processing v1.0. This version does not contain strong SHA-2 message digest function family. This family is supported in v1.1 of the spec. The v.1.1 is a draft at this time. If you want to use strong digest algorithm you have to override default XML Sig implementation by installing Apache XML Security package. The packages have to be installed into endorsed directory of your JRE. There sill remains several drawbacsk. Here they are:

  1. SHA-2 digest family is supported in a standard way but the constants are hidden inside internal class hierarchy

  2. commons-logging fails to initialize due to modified behavior of JRE6 which uses. The chain of commands:this.getClass().getClassLoader() surprisingly returns null as the class from endorsed dir is loaded by bootstrap classloader.



Problem 1) can be slolved by using either strings copied directly from the spec. or by using e.g. org.apache.xml.security.signature.XMLSignature.ALGO_ID_MAC_HMAC_SHA256 constant.

Problem 2) is solved by replacing commons-logging included in XMLSig distribution by newest one (1.1.1) from commons-logging homepage.