Solved

style sheets

Posted on 2012-03-29
8
492 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
  • 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
 
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
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
firstswap challenge 20 67
Java and GPO 11 68
JavaScript/Java - Changing an image background color 4 54
tomcat not starting 6 29
Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Browsing the questions asked to the Experts of this forum, you will be amazed to see how many times people are headaching about monster regular expressions (regex) to select that specific part of some HTML or XML file they want to extract. The examp…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.

911 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now