문제

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");

도움이 되었습니까?

해결책 4

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());
        }

    }

다른 팁

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.

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?

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());
}
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top