0

hi i am very new to Xml parsing

i want to change following attribute values frequently ........... columnCount, width and height

After that i need to rewrite xml file with modified data

in following xml file by using java(sax ,Dom or jaxB parser) please any one can give some suggestion on it...............

=======================================================================================

<?xml version="1.0" encoding="UTF-8"?>
<jasperReport xmlns="http://jasperreports.sourceforge.net/jasperreports" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://jasperreports.sourceforge.net/jasperreports http://jasperreports.sourceforge.net/xsd/jasperreport.xsd" name="Hello_subreport1_subreport1" language="groovy" columnCount="2" printOrder="Horizontal" pageWidth="520" pageHeight="802" columnWidth="260" leftMargin="0" rightMargin="0" topMargin="0" bottomMargin="0" uuid="ac19d62f-eac8-428e-8e0a-9011534189ed">
    <property name="ireport.zoom" value="1.0"/>
    <property name="ireport.x" value="0"/>
    <property name="ireport.y" value="0"/>
    <queryString>
        <![CDATA[]]>
    </queryString>
    <field name="subjectName" class="java.lang.String">
        <fieldDescription><![CDATA[subjectName]]></fieldDescription>
    </field>
    <field name="subjectID" class="java.lang.Integer">
        <fieldDescription><![CDATA[subjectID]]></fieldDescription>
    </field>
    <field name="maxMarks" class="java.lang.Integer">
        <fieldDescription><![CDATA[maxMarks]]></fieldDescription>
    </field>
    <field name="redMarks" class="java.lang.Float">
        <fieldDescription><![CDATA[redMarks]]></fieldDescription>
    </field>
    <field name="passMarks" class="java.lang.Integer">
        <fieldDescription><![CDATA[passMarks]]></fieldDescription>
    </field>
    <field name="marks" class="java.lang.Float">
        <fieldDescription><![CDATA[marks]]></fieldDescription>
    </field>
    <background>
        <band splitType="Stretch"/>
    </background>
    <detail>
        <band height="52">
            <textField isStretchWithOverflow="true">
                <reportElement uuid="5f7665fb-9218-4434-a9e5-5eff306499b3" x="0" y="33" width="100" height="20"/>
                <box>
                    <pen lineWidth="0.0"/>
                    <topPen lineWidth="0.0"/>
                    <leftPen lineWidth="0.0"/>
                    <bottomPen lineWidth="0.0"/>
                    <rightPen lineWidth="0.0"/>
                </box>
                <textElement>
                    <font size="12"/>
                </textElement>
                <textFieldExpression><![CDATA[$F{marks}]]></textFieldExpression>
            </textField>
            <textField>
                <reportElement uuid="6b999cb1-600e-4634-be8f-7ac99e225f49" x="0" y="13" width="100" height="20"/>
                <box>
                    <topPen lineWidth="0.25"/>
                </box>
                <textElement/>
                <textFieldExpression><![CDATA[$F{subjectName}]]></textFieldExpression>
            </textField>
        </band>
    </detail>
</jasperReport>

========================================================================

Andreas Dolk
  • 113,398
  • 19
  • 180
  • 268
venkat
  • 11
  • 1
  • 5

3 Answers3

1

If you want to modify the document them use a DOM parser. This will transform the xml file into a datastructure where you can find the attributes and change their values. Have a look at jdom or dom4j, they are really easy to use.

A sax parser is a good choice if you only want to read the document. That parser just creates events while parsing the document.


Answering to your comment: I do not get a NPE but the rootNode.getChild("detail")) return null. That is because the element is associated with a namespace. Replace the last line in your sample code with

System.out.println(rootNode.getChild("detail", rootNode.getNamespace()));

That works.

Andreas Dolk
  • 113,398
  • 19
  • 180
  • 268
  • i am getting nullpointer exception please provide some solution for that – venkat Nov 08 '12 at 06:39
  • 1
    (are you kidding?) seriously, don't expect to get a solution for a NPE if you don't show any code :-D – Andreas Dolk Nov 08 '12 at 08:22
  • sorry Andreas,Actual i was able to get rootNode and i was able to change attribute of rootNode, but i unable to get child node , it giving null pointer Exception..............please observe below piece of code – venkat Nov 08 '12 at 09:13
  • public class ModifyXMLFile { public static void main(String[] args) { try { SAXBuilder builder = new SAXBuilder(); System.out.println(builder +"builder "); File xmlFile = new File("sample.xml"); Document doc = (Document) builder.build(xmlFile); System.out.println(doc +"builder "); Element rootNode = doc.getRootElement(); rootNode.getAttribute("columnCount").setValue("100"); System.out.println(rootNode +"rootNode"); System.out.println(rootNode.getChild("detail")); --------------->Exactly i am getting NPE here – venkat Nov 08 '12 at 10:07
0

there are tons of documents on web detailing editing of an XML. Explore them and then try something out. If you are stuck then post it here.

some ref: http://www.w3schools.com/dom/default.asp

How to modify XML data in Dom parser

http://www.mkyong.com/java/how-to-modify-xml-file-in-java-dom-parser/

http://www.drdobbs.com/jvm/easy-dom-parsing-in-java/231002580

Community
  • 1
  • 1
Arham
  • 2,072
  • 2
  • 18
  • 30
0

Maybe you could use avc-binding-dom, which binds DOM nodes to your custom Java interface via annotations:

import org.w3c.dom.Node;
import net.avcompris.binding.annotation.XPath;
import net.avcompris.binding.dom.impl.DefaultDomBinder;

@Namespaces("xmlns:jr=http://jasperreports.sourceforge.net/jasperreports")
@XPath("/jr:jasperReport")
interface MyJasperReport {

    @XPath("@columnCount")
    int getColumnCount();
    void setColumnCount(int columnCount);

    @XPath("@pageWidth")
    int getPageWidth();
    void setPageWidth(int pageWidth);

    @XPath("jr:property[@name = 'ireport.zoom']/@value")
    String getZoom();
    void setZoom(String zoom);
}

Node node = ... // You have to load the XML file into a DOM node.

MyJasperReport jr = new DefaultDomBinder().bind(node, MyJasperReport.class);

jr.setColumnCount(4); // previously: 2
jr.setPageWidth(1024); // previously: 520
jr.setZoom("1.45"); // previously: "1.0"

... // then save the DOM node into a XML file.