-1

I have problem with updating data in my xml file.

My xml file looks like this :

<root>
 <info>
 .....
 </info>
  <OBJECT_TYPE>x2000</OBJECT_TYPE>
  <prop>
    <DESCRIPTION>fast train</DESCRIPTION>
    <PARENT>NULL</PARENT>
    <VIRTUAL>0</VIRTUAL>
    <VISIBLE>1</VISIBLE>
    <PICTURE>NULL</PICTURE>
    <HELP>NULL</HELP>
    <MIN_NO>1</MIN_NO>
    <MAX_NO>1</MAX_NO>
    <NAME_FORMAT>NULL</NAME_FORMAT>
  </prop>
<param>
  <PARAMETER>nidbrc</PARAMETER>
  <DATA_TYPE>String</DATA_TYPE>
  <DESCRIPTION>super fast</DESCRIPTION>
  <MIN_NO>1</MIN_NO>
  <MAX_NO>1</MAX_NO>
  <ORDER1>1</ORDER1>
  <NESTED>1</NESTED>
  <DEFAULT1>NULL</DEFAULT1>
  <FORMAT>100:45</FORMAT>
</param>
<param>
</param>
<param>
</param>
<param>
</param>
...
</type>
<type>
  ... 
 </type>
 <type>
</root>

Here i am trying to get my first param from type number 1 and updating the first parameter of 9

public static void main(String[] args) {

        File xml = new File("test.xml");

        try {
            XMLOutputter xmlOut = new XMLOutputter();
            Document doc = (Document) new SAXBuilder().build(xml);
            Element rootNode = doc.getRootElement();
            Element typeContent = rootNode.getChildren().get(1);
            System.out.println("typeContent : " + typeContent.getChildren());

            for (int i = 0; i < typeContent.getContentSize(); i++) {

                List<Element> list = typeContent.getChildren("param");

                if (list.size() > 0) {
                    Element element = list.get(1);
                    List paramChilds = element.getChildren("PARAMETER");

                    for (int j = 0; j < paramChilds.size(); j++) {

                        Element node = (Element) paramChilds.get(j);
                        System.out.println(node.getText());
                        // xmlOut.setFormat(Format.getPrettyFormat());
                        // xmlOut.output(doc, new FileWriter("test.xml"));
                    }
                }
            }

        } catch (IOException io) {
            System.out.println(io.getMessage());
        } catch (JDOMException jdomex) {
            System.out.println(jdomex.getMessage());
        }

What I find hard is to know how to dig into the xml file and therefore this one aint working but this one is null : node.getChild("PARAMETER").setText("Bla");

Sembrano
  • 1,127
  • 4
  • 18
  • 28

4 Answers4

2

You could either loop all 'param' children like this:

Document doc = (Document) new SAXBuilder().build(xml);
Element rootNode = doc.getRootElement();

// get all 'param' children
List<Element> paramElements = root.getChildren("param");
for (Element param: paramElements) {
    // do something intelligent
    param.getChild("PARAMETER").setText("Bla");
}

// write to file
xmlOut.setFormat(Format.getPrettyFormat());
xmlOut.output(doc, new FileWriter("test.xml"));

Or you could use xpath to search for the elements and do stuff with it; example.

R. Oosterholt
  • 7,720
  • 2
  • 53
  • 77
1

Here, you appear to be trying to set the text of the first PARAMETER element which is a child of the first PARAMETER element that is a child of the second child of the root element.

Element rootNode = doc.getRootElement();
...
Element typeContent = rootNode.getChildren().get(1);
...
Element node = typeContent.getChild("PARAMETER");
node.getChild("PARAMETER").setText("Bla");

No such element appears to exist in your example xml.

  1. typeContent corresponds to the element <OBJECT_TYPE>x2000</OBJECT_TYPE>, which has no PARAMETER element children.

  2. There are no PARAMETER elements that are children of PARAMETER elements.

What's more, for some reason you seem to be doing exactly the same thing 15 times. Why is that?

Paul Butcher
  • 6,902
  • 28
  • 39
  • Yes I updated the code but as soon as I am trying to fetch my param child List list = typeContent.getChildren("param"); then its null. Like it doesnt exist. but i deost exist when I am printing it out : System.out.println("typeContent : " + typeContent.getChildren()); – Sembrano Feb 13 '14 at 09:36
  • Are you sure it's null, not an empty list? – Paul Butcher Feb 13 '14 at 09:39
  • Its typing out like this : typeContent : [[Element: ], [Element: ], [Element: ], [Element: ], [Element: ], [Element: ], [Element: ], [Element: ], [Element: – Sembrano Feb 13 '14 at 09:44
  • Where did that namespace come from? It's not mentioned in your example XML. You will need to use the two-argument form of [getChildren](http://www.jdom.org/docs/apidocs/org/jdom2/Element.html#getChildren()), in order to specify the namespace. – Paul Butcher Feb 13 '14 at 09:47
  • Dunno I guess its from my root tag that has some xmlns stuffs and links to schema. – Sembrano Feb 13 '14 at 09:49
  • There are no namespace declarations on the root tag in your example. If the real xml has namespaces, then these are not irrelevant. You will need to use the value from xmlns="" as the second argument. – Paul Butcher Feb 13 '14 at 09:56
  • so ill do like this? or do I need to use them all? : Namespace ns = Namespace.getNamespace("http://www.bombardier.com"); – Sembrano Feb 13 '14 at 10:04
1

You are looking for the param Elements like this:

List<Element> list = typeContent.getChildren("param");

But typeContent does not have any param children. typeContent is:

Element typeContent = rootNode.getChildren().get(1);

which, as far as I can tell, is:

<OBJECT_TYPE>x2000</OBJECT_TYPE>

You should, I guess, be looking for the param children like:

List<Element> paramElements = root.getChildren("param");

You should be using JDOM2 and not JDOM. With JDOM2 your XPath option is much simpler:

XPathExpression<Element> paramxpath = XPathFactory.instance()
         .compile("/root/param", Filters.element());
for (Element param : paramxpath.evaluate(doc)) {
    System.out.println(param.getText());
}
rolfl
  • 17,539
  • 7
  • 42
  • 76
0

This is my complete solution :

public void updateParameters(int index, int row, int column,
            String columnName, Object data) throws Exception {

        int objTypeIndex = index + 1;

        File xml = new File("xmlFiles/CoreDatamodel.xml");

        try {
            XMLOutputter xmlOut = new XMLOutputter();
            org.jdom2.Document doc = new SAXBuilder().build(xml);
            Namespace ns = Namespace.getNamespace("http://www.bombardier.com");
            org.jdom2.Element rootNode = doc.getRootElement();
            org.jdom2.Element typeContent = rootNode.getChildren().get(
                    objTypeIndex);

            List<Element> list = typeContent.getChildren("param", ns);

            if (list.size() > 0) {
                Element element = list.get(row);
                List paramChilds = element.getChildren(columnName, ns);

                Element node = (Element) paramChilds.get(0);
                node.setText(data.toString());
                System.out.println(node.getText());
                xmlOut.setFormat(Format.getPrettyFormat());
                xmlOut.output(doc, new FileWriter("xmlFiles/CoreDatamodel.xml"));

            }

        } catch (IOException io) {
            System.out.println(io.getMessage());
        } catch (JDOMException jdomex) {
            System.out.println(jdomex.getMessage());
        }

    }
Sembrano
  • 1,127
  • 4
  • 18
  • 28