在xml中编辑更多名称

时间:2013-06-03 09:05:29

标签: java xml jtable sax

我正在使用xml我已经使用了sax解析器我必须用指定的格式解析xml中的数据现在的问题是nameattribute的名称我必须只显示它的一半,直到现在它显示整个名称。如何从xml中删除选择性字符。由于我是xml和java的新手,我对此感到困惑。 我的代码如下所示:

import java.util.ArrayList;
import java.util.List;
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

public class XmlBack extends DefaultHandler {

    public void startElement(String uri, String localName, String qName,
            Attributes attributes) throws SAXException {

        if (qName.equalsIgnoreCase("VarGroup")) {
            varGroupVariables = new ArrayList<VarGroupVariable>();
        }else if (qName.equalsIgnoreCase("Variable")) {
            varGroupVariable = new VarGroupVariable();
            nameAttribute = attributes.getValue("Name");
            varGroupVariable.setName(nameAttribute);
        }else if (qName.equalsIgnoreCase("TYP")) {
            btype = true;
        }else if (qName.equalsIgnoreCase("VALUE")) {
            bvalue = true;
        }else if (qName.equalsIgnoreCase("Mold.sv_")) {
            bmold = true;
        }else if (qName.equalsIgnoreCase("Core1.sv_")) {
            bcore = true;
        }

    }

    public void characters(char ch[], int start, int length)
            throws SAXException {
        String vtype = null;

        if (bcore) {

            bcore = false;
        }
        if (bmold) {
            bmold = true;
        }
        if (btype) {
            vtype = new String(ch, start, length);
            varGroupVariable.setType(vtype);
            varGroupVariable.setSerial(no++);
            btype = false;
        }

        Double value = null;
        if (bvalue) {
            String vvalue = new String(ch, start, length);
            try {
                value = Double.valueOf(vvalue);
            } catch (NumberFormatException ne) {
                value = 0d;
            }
            varGroupVariable.setValue(value);
            bvalue = false;
        }
    }

    @Override
    public void endElement(String uri, String localName, String qName)
            throws SAXException {
        if (qName.equalsIgnoreCase("Variable")) {
            varGroupVariables.add(varGroupVariable);
        }
    }

    private int no = 1;
    boolean bcore,bmold,bvalue,btype = false;
    String nameAttribute;
    List<VarGroupVariable> varGroupVariables;
    VarGroupVariable varGroupVariable;
}

My sample xml is provided below

<?xml version="1.0" encoding="UTF-8"?>
<HMI_Data Version="1.0" MaschinenNR.="XXXXXX" Date="21-10-2009">
   <VarGroup Name="VG_MachineData">
      <Variable Name="Mold1.sv_rMoldStroke">
         <Typ>REAL</Typ>
         <Value>6.000000e+02</Value>
      </Variable>
      <Variable Name="Core1.sv_rMaxSpeedFwd"> //REMOVE core1.sv_
         <Typ>REAL</Typ>
         <Value>5.000000e+01</Value>
      </Variable>
      <Variable Name="Core1.sv_rMaxSpeedBwd">
         <Typ>REAL</Typ>
         <Value>5.000000e+01</Value>
      </Variable>
      <Variable Name="Core1.sv_rMaxPressureFwd">
         <Typ>REAL</Typ>
         <Value>1.450000e+02</Value>
      </Variable>
      <Variable Name="Core1.sv_rMaxPressureBwd">
         <Typ>REAL</Typ>
         <Value>1.450000e+02</Value>
      </Variable>
      <Variable Name="Core1.sv_rImpulseFilterLimit">
         <Typ>REAL</Typ>
         <Value>0.000000e+00</Value>
      </Variable>
      <Variable Name="Core2.sv_rMaxSpeedFwd"> //REMOVE core2.sv_
         <Typ>REAL</Typ>
         <Value>5.000000e+01</Value>
      </Variable>
      <Variable Name="Core2.sv_rMaxSpeedBwd">
         <Typ>REAL</Typ>
         <Value>5.000000e+01</Value>
      </Variable>

1 个答案:

答案 0 :(得分:0)

尝试使用

nameAttribute = attributes.getValue("Name").split("_")[1];