我正在處理xml我已經使用了sax解析器我必須從指定格式解析xml中的數據現在的問題是nameattribute具有名稱的位置我必須顯示它的一半直到現在它顯示整個名稱。我如何從XML中刪除選擇性字符。由於我是新來的XML和Java我有點困惑。 我的代碼如下所示:在xml中編輯更多的名字
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>
你可以給使用示例XML節點例如所以,這將是很容易理解你的問題 –
我在示例XML保持評論要刪除的字符串。感謝您的評論。它顯示全名如Core1.sv_rMaxSpeedFwd,我必須從它刪除Core1.sv我該怎麼做 – ZED