Solved

style sheets

Posted on 2012-03-29
8
496 Views
Last Modified: 2012-08-14
Can style sheets be used to filter xml data to basically hide certain fields?
Example:
If I wanted to filter the following xml below to only have <fielda>?
<test>
  <fielda>abc</fielda>
  <fieldb>123</fieldb>
</test>
0
Comment
Question by:dpalyca755
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 3
8 Comments
 
LVL 60

Assisted Solution

by:Geert Bormans
Geert Bormans earned 188 total points
ID: 37785313
You can use an XSLT stylesheet to transform one XML into another XML.
And yes you can programm the stylesheet in a way that it keeps all the elements intact but filter out fieldb... or any other logic you would want.

XSLT stylesheets area very powerfull mechanism to manipulate XML trees
0
 
LVL 47

Assisted Solution

by:for_yan
for_yan earned 312 total points
ID: 37785316
I think this gives several examples of how to remove some of the nodes from XML:
http://stackoverflow.com/questions/3717215/remove-xml-node-using-java-parser
0
 
LVL 47

Assisted Solution

by:for_yan
for_yan earned 312 total points
ID: 37785321
this gives the code how to remove the element:

from

http://www.kodejava.org/examples/479.html

package org.kodejava.example.jdom;

import org.jdom.Document;
import org.jdom.JDOMException;
import org.jdom.output.XMLOutputter;
import org.jdom.output.Format;
import org.jdom.input.SAXBuilder;

import java.io.File;
import java.io.IOException;

public class JDOMRemoveElement {
    public static void main(String[] args) {
        SAXBuilder builder = new SAXBuilder();
        try {
            Document doc = builder.build(new File("userinfo.xml"));

            //
            // The lines below output the original userinfo.xml content
            //
            // <?xml version="1.0" encoding="UTF-8"?>
            // <rows>
            //   <row>
            //     <firstname>Alice</firstname>
            //     <lastname>Mallory</lastname>
            //     <address>Sunset Road</address>
            //   </row>
            // </rows>
            //            
            XMLOutputter out = new XMLOutputter(Format.getPrettyFormat());
            out.output(doc, System.out);

            //
            // Remove the address element from Alice information. First we
            // get the row element from the root element, and finally 
            // remove the address from the row. And the result will be:
            //
            // <?xml version="1.0" encoding="UTF-8"?>
            // <rows>
            //   <row>
            //     <firstname>Alice</firstname>
            //     <lastname>Mallory</lastname>
            //   </row>
            // </rows>
            //
            doc.getRootElement().getChild("row").removeChild("address");
            out.output(doc, System.out);
        } catch (JDOMException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

Open in new window

0
Resolve Critical IT Incidents Fast

If your data, services or processes become compromised, your organization can suffer damage in just minutes and how fast you communicate during a major IT incident is everything. Learn how to immediately identify incidents & best practices to resolve them quickly and effectively.

 
LVL 60

Accepted Solution

by:
Geert Bormans earned 188 total points
ID: 37785333
Given that the question was "can stylesheets do this?" ...
here is an example of a stylesheet with two templates
one for general copy and continue processing (identity transform)
and one template that removes the fieldb

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    version="1.0">
    <xsl:output indent="yes"/>
    <xsl:strip-space elements="*"/>
    
    <xsl:template match="node()">
        <xsl:copy>
            <xsl:copy-of select="@*"/>
            <xsl:apply-templates select="node()"/>
        </xsl:copy>
    </xsl:template>
    
    <xsl:template match="fieldb"/>
</xsl:stylesheet>

Open in new window

0
 
LVL 60

Assisted Solution

by:Geert Bormans
Geert Bormans earned 188 total points
ID: 37785341
And here is one that removes all but the fielda element

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    version="1.0">
    <xsl:output indent="yes"/>
    <xsl:strip-space elements="*"/>
    
    <xsl:template match="/">
           <xsl:copy-of select="//fielda"/>
    </xsl:template>

</xsl:stylesheet>

Open in new window


Note that you can parameterize that so that you can pass in the name of the element you want to filter out

generally simple tasks like this could be done in java code directly. XML processing rapidly reaches a complexity that is best done in XSLT
0
 
LVL 47

Assisted Solution

by:for_yan
for_yan earned 312 total points
ID: 37785347
I tested this code:

import org.jdom.Document;
import org.jdom.JDOMException;
import org.jdom.output.XMLOutputter;
import org.jdom.output.Format;
import org.jdom.input.SAXBuilder;


import java.io.File;
import java.io.IOException;

public class JDOMRemoveElement {
    public static void main(String[] args) {
        SAXBuilder builder = new SAXBuilder();
        try {
            org.jdom.Document doc = builder.build(new File("yourXML.xml"));


            XMLOutputter out = new XMLOutputter(Format.getPrettyFormat());
       //     out.output(doc, System.out);

            
            doc.getRootElement().getChild("test").removeChild("fieldb");
            out.output(doc, System.out);
        } catch (JDOMException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

Open in new window



Input:

<?xml version="1.0" encoding="UTF-8"?>
<root>
<test>
  <fielda>abc</fielda>
  <fieldb>123</fieldb>
</test>
</root>

Open in new window


Output:

<?xml version="1.0" encoding="UTF-8"?>
<root>
  <test>
    <fielda>abc</fielda>
  </test>
</root>

Open in new window

0
 
LVL 47

Assisted Solution

by:for_yan
for_yan earned 312 total points
ID: 37785356
This code takes input exactly as you posted it and removes filedb

import org.jdom.Document;
import org.jdom.JDOMException;
import org.jdom.output.XMLOutputter;
import org.jdom.output.Format;
import org.jdom.input.SAXBuilder;


import java.io.File;
import java.io.IOException;

public class JDOMRemoveElement {
    public static void main(String[] args) {
        SAXBuilder builder = new SAXBuilder();
        try {
            org.jdom.Document doc = builder.build(new File("yourXML.xml"));


            XMLOutputter out = new XMLOutputter(Format.getPrettyFormat());
           // out.output(doc, System.out);

            
            doc.getRootElement().removeChild("fieldb");
            out.output(doc, System.out);
        } catch (JDOMException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

Open in new window

Input:
<test>
  <fielda>abc</fielda>
  <fieldb>123</fieldb>
</test>

Open in new window


Output:

<?xml version="1.0" encoding="UTF-8"?>
<test>
  <fielda>abc</fielda>
</test>

Open in new window

0
 
LVL 47

Assisted Solution

by:for_yan
for_yan earned 312 total points
ID: 37785388
and this code does the same thing as above but writes to the output file:

You need to download
http://www.jdom.org/dist/binary/jdom-1.1.3.zip

and then extract jar flile jdom-1.1.3.jar
and put it in the classpath


import org.jdom.Document;
import org.jdom.JDOMException;
import org.jdom.output.XMLOutputter;
import org.jdom.output.Format;
import org.jdom.input.SAXBuilder;


import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;

public class JDOMRemoveElement {
    public static void main(String[] args) {
        SAXBuilder builder = new SAXBuilder();
        try {
            org.jdom.Document doc = builder.build(new File("yourXML.xml"));


            XMLOutputter out = new XMLOutputter(Format.getPrettyFormat());
         //   out.output(doc, System.out);

            
            doc.getRootElement().removeChild("fieldb");
            out.output(doc, new FileOutputStream("yourXML1.xml"));
        } catch (JDOMException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

Open in new window


Input file:

<test>
  <fielda>abc</fielda>
  <fieldb>123</fieldb>
</test>

Open in new window


Output file:

<?xml version="1.0" encoding="UTF-8"?>
<test>
  <fielda>abc</fielda>
</test>

Open in new window

0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
Suggested Courses

751 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question