<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:jee="http://www.springframework.org/schema/jee" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation="    http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd    http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd    http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-2.5.xsd    http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
<bean id="largeParameter" class="com.alibaba.omeo.captcha.service.VariableParameter">
<property name="width" value="150"/>
<property name="height" value="40"/>
<property name="GMinFontSize" value="34"/>
<property name="GMaxFontSize" value="34"/>
<property name="SMinFontSize" value="32"/>
<property name="SMaxFontSize" value="32"/>
<property name="HMinFontSize" value="30"/>
<property name="HMaxFontSize" value="30"/>
<property name="overlapPixel" value="2"/>
<property name="horizontalMargin" value="5"/>
<property name="verticalMargin" value="5"/>
<property name="radius" value="40"/>
<property name="XAmplitude" value="5"/>
<property name="YAmplitude" value="5"/>
<property name="XWaveLength" value="20"/>
<property name="YWaveLength" value="10"/>
<property name="scale" value="40"/>
<property name="amount" value="8"/>
<property name="turbulence" value="8"/>
<property name="time" value="90"/>
</bean>
<bean id="randomFontGenerator" class="com.alibaba.omeo.captcha.component.font.RandomFontGenerator">
<property name="fonts">
<list>
<value>Bitstream Charter</value>
<value>Century Schoolbook L</value>
<value>Courier 10 Pitch</value>
<value>Bitstream Vera Serif</value>
<value>URW Bookman L</value>
</list>
</property>
</bean>
<bean id="randomRangeColorGenerator" class="com.alibaba.omeo.captcha.component.color.RandomRangeColorGenerator">
<property name="redComponentRange">
<list>
<value>150</value>
<value>255</value>
</list>
</property>
<property name="greenComponentRange">
<list>
<value>150</value>
<value>255</value>
</list>
</property>
<property name="blueComponentRange">
<list>
<value>150</value>
<value>255</value>
</list>
</property>
<property name="alphaComponentRange">
<list>
<value>150</value>
<value>255</value>
</list>
</property>
</bean>
</beans>上边是xml文件,修改<property name="width" value="150"/>中的value中的值,有好多property怎么确定是这个<property name="width" value="150"/>;还有<property name="redComponentRange">
<list>
<value>150</value>
<value>255</value>
</list>
</property>中<value>150</value>的值;感谢各位了

解决方案 »

  1.   


    Hashtable<String, Double> hashtable = new Hashtable<String, Double>();
    ......try{
          SAXReader saxReader = new SAXReader();
          Document document = saxReader.read(inputXml);
          List list = document.selectNodes("//bean/property" );
          Iterator iter=list.iterator();
          while(iter.hasNext()){
              Element element=(Element)iter.next();
              hashtable.put(element.elementText("name"), new Double(element.elementText("value")))
          }
          if(hashtable.containsKey("width")){
              ......
          }
       }      
      

  2.   

    <bean id="largeParameter" class="com.alibaba.omeo.captcha.service.VariableParameter">
            <property name="width" value="150"/>
            <property name="height" value="40"/>
            <property name="GMinFontSize" value="34"/>
            <property name="GMaxFontSize" value="34"/>
            <property name="SMinFontSize" value="32"/>
            <property name="SMaxFontSize" value="32"/>
            <property name="HMinFontSize" value="30"/>
            <property name="HMaxFontSize" value="30"/>
            <property name="overlapPixel" value="2"/>
            <property name="horizontalMargin" value="5"/>
            <property name="verticalMargin" value="5"/>
            <property name="radius" value="40"/>
            <property name="XAmplitude" value="5"/>
            <property name="YAmplitude" value="5"/>
            <property name="XWaveLength" value="20"/>
            <property name="YWaveLength" value="10"/>
            <property name="scale" value="40"/>
            <property name="amount" value="8"/>
            <property name="turbulence" value="8"/>
            <property name="time" value="90"/>
        </bean>
    这样的节点不止一个,怎么确定是id=largeParameter的这个节点呢;另外,修改后保存在Hashtable中的我该怎么保存回去
      

  3.   

    用xpath定位?
    /beans/bean[@id='largeParameter']/property[@name='width']
    这样就可以定位到你要的property
      

  4.   


     Document doc = new SAXReader().read(XXX.class.getResourceAsStream("/xxx.xml"));
     Element beanEle = (Element) doc.selectSingleNode("//bean[@id=largeParameter]");
     Element proEle = (Element) beanEle.selectSingleNode("//property[@name=width]");
     ......这样应该OK吧,试试看
      

  5.   

    ok搞定了,贴出来大伙看哈;public static void main(String[] args) {
    Document doc = null;
    // TODO 自动生成方法存根
    String xstr = "D:\\eclipse_workspace\\ImageServer\\src\\com\\aliyun\\security\\service\\image.xml";
    // 使应用程序能够从 XML 文档获取生成 DOM 对象树的解析器
    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
    try {
    // 使其从 XML 文档获取 DOM 文档实例
    DocumentBuilder db = dbf.newDocumentBuilder();
    File f = new File(xstr);
    doc = db.parse(f);
    } catch (ParserConfigurationException e) {
    // TODO 自动生成 catch 块
    e.printStackTrace();
    System.out.println("无法创建DocumentBuilder!!");
    } catch (SAXException e) {
    // TODO 自动生成 catch 块
    e.printStackTrace();
    System.out.println("该文件不是XML文件!!");
    } catch (IOException e) {
    // TODO 自动生成 catch 块
    System.out.println("文件读写时发生了错误!!");
    e.printStackTrace();
    }
    // 格式化文档
    doc.getDocumentElement().normalize();
    System.out.println("XML 文件结构正确!");
    // 得到根结点
    Node root = doc.getFirstChild();
    // Element root = doc.getDocumentElement();
    System.out.println("根结点是:" + root.getNodeName()); if (root.getNodeType() == Node.ELEMENT_NODE) {
    NodeList nodelist = root.getChildNodes();
    for (int i = 0; i < nodelist.getLength(); i++) {
    Node node = nodelist.item(i);
    if (node.getNodeType() == Node.ELEMENT_NODE) {
    if (node.getNodeName().equals("bean")) {
    if (node.getAttributes().getNamedItem("id")
    .getNodeValue().equals("largeParameter")) {
    NodeList nodelist1 = node.getChildNodes();
    for (int j = 0; j < nodelist1.getLength(); j++) {
    Node node1 = nodelist1.item(j);
    if (node1.getNodeType() == Node.ELEMENT_NODE) {
    if (node1.getNodeName().equals("property")) {
    if (node1.getAttributes()
    .getNamedItem("name")
    .getNodeValue().equals("width")) {
    node1.getAttributes()
    .getNamedItem("value")
    .setNodeValue("00000");
    }
    }
    }
    }
    }
    }
    }
    }
    } try {
    TransformerFactory tfactory = TransformerFactory.newInstance();
    Transformer transformer = tfactory.newTransformer();
    DOMSource source = new DOMSource(root);
    /* 新文件的地址 */
    File file = new File(
    "D:\\eclipse_workspace\\ImageServer\\src\\com\\aliyun\\security\\service\\image.xml");
    StreamResult result = new StreamResult(file);
    transformer.transform(source, result);
    } catch (TransformerException e) {
    e.printStackTrace();
    }
    }
      

  6.   

    这个是我的xml文件<?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
    xmlns:aop="http://www.springframework.org/schema/aop" xmlns:jee="http://www.springframework.org/schema/jee"
    xmlns:tx="http://www.springframework.org/schema/tx"
    xsi:schemaLocation="
    http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
    http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
    http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-2.5.xsd
    http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
    <bean id="largeParameter" class="com.alibaba.omeo.captcha.service.VariableParameter">
    <property name="width" value="150" />
    <property name="height" value="40" />
    <property name="GMinFontSize" value="34" />
    <property name="GMaxFontSize" value="34" />
    <property name="SMinFontSize" value="32" />
    <property name="SMaxFontSize" value="32" />
    <property name="HMinFontSize" value="30" />
    <property name="HMaxFontSize" value="30" />
    <property name="overlapPixel" value="2" />
    <property name="horizontalMargin" value="5" />
    <property name="verticalMargin" value="5" />
    <property name="radius" value="40" />
    <property name="XAmplitude" value="5" />
    <property name="YAmplitude" value="5" />
    <property name="XWaveLength" value="20" />
    <property name="YWaveLength" value="10" />
    <property name="scale" value="40" />
    <property name="amount" value="8" />
    <property name="turbulence" value="8" />
    <property name="time" value="90" />
    </bean> <bean id="smallParameter" class="com.alibaba.omeo.captcha.service.VariableParameter">
    <property name="width" value="100" />
    <property name="height" value="30" />
    <property name="GMinFontSize" value="26" />
    <property name="GMaxFontSize" value="26" />
    <property name="SMinFontSize" value="23" />
    <property name="SMaxFontSize" value="24" />
    <property name="HMinFontSize" value="22" />
    <property name="HMaxFontSize" value="22" />
    <property name="overlapPixel" value="0.05" />
    <property name="horizontalMargin" value="5" />
    <property name="verticalMargin" value="5" />
    <property name="radius" value="0" />
    <property name="XAmplitude" value="5" />
    <property name="YAmplitude" value="4" />
    <property name="XWaveLength" value="20" />
    <property name="YWaveLength" value="10" />
    <property name="scale" value="40" />
    <property name="amount" value="8" />
    <property name="turbulence" value="8" />
    <property name="time" value="90" />
    </bean>
      

  7.   

    太麻烦了,用xpath定位?
    /beans/bean[@id='largeParameter']/property[@name='width']
    这样就可以定位到你要的property,6楼这位兄弟是比较简单的做法,很容易就能定位到你要改的节点,不用一层一层去遍历
      

  8.   

    Element root = document.getRootElement(); // 获得根元素
    List<Node> nodes = root.selectNodes("/beans/bean/property[@name='width' and @value='150']");使用XPath来定位是最快捷的,网上有很多XPath的教程 !  你自己看看就明白了 !