使用DOM4J更改XML属性的最佳方法是什么

时间:2014-02-06 18:10:19

标签: java xml dom4j

我试图用DOM4J改变XML文件的值。该文件不是太大,但它有很多标签:

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<process version="5.3.013">
  <context>
    <input/>
    <output/>
    <macros/>
  </context>
  <operator activated="true" class="process" compatibility="5.3.013" expanded="true" name="Process">
    <parameter key="logverbosity" value="init"/>
    <parameter key="random_seed" value="2001"/>
    <parameter key="send_mail" value="never"/>
    <parameter key="notification_email" value=""/>
    <parameter key="process_duration_for_mail" value="30"/>
    <parameter key="encoding" value="SYSTEM"/>
    <process expanded="true">
      <operator activated="true" class="read_csv" compatibility="5.3.013" expanded="true" height="60" name="Read CSV" width="90" x="45" y="30">
        <parameter key="csv_file" value="C:\Documents and Settings\Geral\workspace\AdaptDBAnalisysPlatform_Core\Results\QueryResults\P1\Results1.csv"/>
        <parameter key="column_separators" value=","/>
        <parameter key="trim_lines" value="false"/>
        <parameter key="use_quotes" value="true"/>
        <parameter key="quotes_character" value="&quot;"/>
        <parameter key="escape_character" value="\"/>
        <parameter key="skip_comments" value="false"/>
        <parameter key="comment_characters" value="#"/>
        <parameter key="parse_numbers" value="true"/>
        <parameter key="decimal_character" value="."/>
        <parameter key="grouped_digits" value="false"/>
        <parameter key="grouping_character" value=","/>
        <parameter key="date_format" value=""/>
        <parameter key="first_row_as_names" value="false"/>
        <list key="annotations">
          <parameter key="0" value="Name"/>
        </list>
        <parameter key="time_zone" value="SYSTEM"/>
        <parameter key="locale" value="English (United States)"/>
        <parameter key="encoding" value="windows-1252"/>
        <list key="data_set_meta_data_information">
          <parameter key="0" value="idUSER.true.integer.id"/>
          <parameter key="1" value="GENERO.true.integer.attribute"/>
          <parameter key="2" value="IDADE.true.binominal.attribute"/>
          <parameter key="3" value="PALAVRACHAVE.true.polynominal.label"/>
        </list>
        <parameter key="read_not_matching_values_as_missings" value="true"/>
        <parameter key="datamanagement" value="double_array"/>
      </operator>
      <operator activated="true" class="set_role" compatibility="5.3.013" expanded="true" height="76" name="Set Role" width="90" x="45" y="165">
        <parameter key="attribute_name" value="PALAVRACHAVE"/>
        <parameter key="target_role" value="label"/>
        <list key="set_additional_roles">
          <parameter key="idUSER" value="id"/>
        </list>
      </operator>
      <operator activated="true" class="k_medoids" compatibility="5.3.013" expanded="true" height="76" name="Clustering (2)" width="90" x="246" y="30">
        <parameter key="add_cluster_attribute" value="true"/>
        <parameter key="add_as_label" value="true"/>
        <parameter key="remove_unlabeled" value="false"/>
        <parameter key="k" value="4"/>
        <parameter key="max_runs" value="100"/>
        <parameter key="max_optimization_steps" value="100"/>
        <parameter key="use_local_random_seed" value="false"/>
        <parameter key="local_random_seed" value="1992"/>
        <parameter key="measure_types" value="MixedMeasures"/>
        <parameter key="mixed_measure" value="MixedEuclideanDistance"/>
        <parameter key="nominal_measure" value="DiceSimilarity"/>
        <parameter key="numerical_measure" value="EuclideanDistance"/>
        <parameter key="divergence" value="GeneralizedIDivergence"/>
        <parameter key="kernel_type" value="radial"/>
        <parameter key="kernel_gamma" value="1.0"/>
        <parameter key="kernel_sigma1" value="1.0"/>
        <parameter key="kernel_sigma2" value="0.0"/>
        <parameter key="kernel_sigma3" value="2.0"/>
        <parameter key="kernel_degree" value="3.0"/>
        <parameter key="kernel_shift" value="1.0"/>
        <parameter key="kernel_a" value="1.0"/>
        <parameter key="kernel_b" value="0.0"/>
      </operator>
      <operator activated="true" class="map_clustering_on_labels" compatibility="5.3.013" expanded="true" height="76" name="Map Clustering on Labels" width="90" x="313" y="165"/>
      <connect from_op="Read CSV" from_port="output" to_op="Set Role" to_port="example set input"/>
      <connect from_op="Set Role" from_port="example set output" to_op="Clustering (2)" to_port="example set"/>
      <connect from_op="Clustering (2)" from_port="cluster model" to_op="Map Clustering on Labels" to_port="cluster model"/>
      <connect from_op="Clustering (2)" from_port="clustered set" to_op="Map Clustering on Labels" to_port="example set"/>
      <connect from_op="Map Clustering on Labels" from_port="example set" to_port="result 1"/>
      <portSpacing port="source_input 1" spacing="0"/>
      <portSpacing port="sink_result 1" spacing="0"/>
      <portSpacing port="sink_result 2" spacing="0"/>
    </process>
  </operator>
</process>

这个想法很简单。我想更改行<parameter key="k" value="4"/>的值   (对于k_medoids运算符)的另一个值。因此,我想了下面的代码http://www.ibm.com/developerworks/xml/library/x-dom4j/index.html?ca=drs

   PARAMETERS_Key = document.selectNodes("//operator/parameter/@key" ); //find parameter K
             PARAMETERS_Key_IT = PARAMETERS_Key.iterator();

             while(PARAMETERS_Key_IT.hasNext())
             {
                PARAMETERS_Value = document.selectNodes("//operator/parameter/@value" ); //Value of the parameter
                PARAMETERS_Value_IT = PARAMETERS_Value.iterator();

                Attribute KEY=(Attribute)PARAMETERS_Key_IT.next();
                Attribute VALUE=(Attribute)PARAMETERS_Value_IT.next();

                 if(KEY.getValue().equals("k"))  //if Parameter key is k, then change its value for 4
                     VALUE.setValue("41000"); 
             }

             XMLWriter output = new XMLWriter(new FileWriter( new File("c:/catalog/catalog-modified.xml") ));
             output.write( document );
             output.close();

显然,Xpath是正确的:通过http://www.xpathtester.com/test确认,但是我得到以下异常,表明显然Xpath不正常:

Exception in thread "main" java.lang.NoClassDefFoundError: org/jaxen/JaxenException
    at org.dom4j.DocumentFactory.createXPath(DocumentFactory.java:230)
    at org.dom4j.tree.AbstractNode.createXPath(AbstractNode.java:207)
    at org.dom4j.tree.AbstractNode.selectNodes(AbstractNode.java:164)
    at general.XMLParser.modifyDocument(XMLParser.java:38)
    at dataMinning.processes.P1.<init>(P1.java:15)
    at dataMining.ModelCaller.Model1(ModelCaller.java:22)
    at dataMining.ModelCaller.work(ModelCaller.java:16)
    at general.Begin.main(Begin.java:22)
Caused by: java.lang.ClassNotFoundException: org.jaxen.JaxenException
    at java.net.URLClassLoader$1.run(Unknown Source)
    at java.net.URLClassLoader$1.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(Unknown Source)
    at java.lang.ClassLoader.loadClass(Unknown Source)
    at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
    at java.lang.ClassLoader.loadClass(Unknown Source)
    ... 8 more

我没有看到任何问题。我错过了什么?

提前感谢

1 个答案:

答案 0 :(得分:1)

这个问题使用W3C DOM更容易解决,正如keshlam建议的那样。 这是我将值4更新为行<parameter key="k" value="4"/>

中的另一个值的解决方案
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.IOException;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;*/


import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;



import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathConstants;
import javax.xml.xpath.XPathExpressionException;
import javax.xml.xpath.XPathFactory;



import org.w3c.dom.Document;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerConfigurationException;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;

import org.w3c.dom.Element;




public class XMLParser {


     public void modifyDocument(String inputXml)
     {


                 try {
                     FileInputStream file = new FileInputStream(new File(inputXml));

                     DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance();

                     DocumentBuilder builder =  builderFactory.newDocumentBuilder();

                     Document xmlDocument = builder.parse(file);

                     XPath xPath =  XPathFactory.newInstance().newXPath();

                     String expression = "//operator[@class='k_medoids']/parameter[@key='k']";
                     String email = xPath.compile(expression).evaluate(xmlDocument);

                     NodeList nodeList = (NodeList) xPath.compile(expression).evaluate(xmlDocument, XPathConstants.NODESET);

                     System.out.println(nodeList.getLength());
                     for (int i = 0; i < nodeList.getLength(); i++) {

                         NamedNodeMap attr = nodeList.item(i).getAttributes();
                        Node nodeAttr = attr.getNamedItem("value");
                        nodeAttr.setTextContent("VALUE TO CHANGE"); 
                     }

                  // write the content into xml file
                        TransformerFactory transformerFactory = TransformerFactory.newInstance();
                        Transformer transformer = transformerFactory.newTransformer();
                        DOMSource source = new DOMSource(xmlDocument);
                        StreamResult result = new StreamResult(new File(inputXml));
                        transformer.transform(source, result);              

                 } catch (FileNotFoundException e) {
                     e.printStackTrace();
                 } catch (SAXException e) {
                     e.printStackTrace();
                 } catch (IOException e) {
                     e.printStackTrace();
                 } catch (ParserConfigurationException e) {
                     e.printStackTrace();
                 } catch (XPathExpressionException e) {
                     e.printStackTrace();
                 } catch (TransformerConfigurationException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                } catch (TransformerException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }       
             }

}

我必须说XPath非常有用。

最好的问候