使用R的xml2包解析CPLEX XML:提取变量名、索引与值
Solution to Extract CPLEX Variable Details Using xml2 in R
You’re really close! You’ve already targeted the <variables> node—now we just need to drill down to each individual <variable> element and pull out their attributes (name, index, value). Here’s how to do it smoothly:
Step-by-Step Code
require(xml2) # Your existing XML content and setup xml_file <- '<?xml version = "1.0" encoding="UTF-8" standalone="yes"?> <CPLEXSolution version="1.2"> <header problemName="Oil-blending.lp" objectiveValue="287750" solutionTypeValue="1" solutionTypeString="basic" solutionStatusValue="1" solutionStatusString="optimal" solutionMethodString="dual" primalFeasible="1" dualFeasible="1" simplexIterations="14" writeLevel="1"/> <quality epRHS="1e-06" epOpt="1e-06" maxPrimalInfeas="0" maxDualInfeas="0" maxPrimalResidual="9.66338120633736e-13" maxDualResidual="7.105427357601e-15" maxX="7500" maxPi="57.25" maxSlack="4000" maxRedCost="40.9" kappa="83.7880434782609"/> <linearConstraints> <constraint name="ct_demand({"Super"})" index="0" status="LL" slack="0" dual="-20.8"/> <constraint name="ct_demand({"Regular"})" index="1" status="LL" slack="0" dual="0.1"/> <constraint name="ct_demand({"Diesel"})" index="2" status="LL" slack="0" dual="-40.8"/> <constraint name="ct_capacity({"Crude1"})" index="3" status="LL" slack="0" dual="57.25"/> <constraint name="ct_capacity({"Crude2"})" index="4" status="LL" slack="0" dual="20.9"/> <constraint name="ct_capacity({"Crude3"})" index="5" status="BS" slack="1500" dual="0"/> <constraint name="ct_total_max_prod" index="6" status="BS" slack="499.999999999997" dual="0"/> <constraint name="ct_octane_min({"Super"})" index="7" status="BS" slack="-2000" dual="-0"/> <constraint name="ct_octane_min({"Regular"})" index="8" status="LL" slack="0" dual="-1.77635683940025e-15"/> <constraint name="ct_octane_min({"Diesel"})" index="9" status="BS" slack="-4000" dual="-0"/> <constraint name="ct_lead_max({"Super"})" index="10" status="LL" slack="0" dual="30.9"/> <constraint name="ct_lead_max({"Regular"})" index="11" status="LL" slack="0" dual="30.9"/> <constraint name="ct_lead_max({"Diesel"})" index="12" status="LL" slack="0" dual="30.9"/> </linearConstraints> <variables> <variable name="Blend({"Crude1"})({"Super"})" index="0" status="BS" value="2222.22222222222" reducedCost="-0"/> <variable name="Blend({"Crude2"})({"Super"})" index="1" status="BS" value="444.444444444444" reducedCost="-0"/> <variable name="Blend({"Crude3"})({"Super"})" index="2" status="BS" value="333.333333333333" reducedCost="-0"/> <variable name="Blend({"Crude1"})({"Regular"})" index="3" status="BS" value="2111.11111111111" reducedCost="-0"/> <variable name="Blend({"Crude2"})({"Regular"})" index="4" status="BS" value="4222.22222222222" reducedCost="-0"/> <variable name="Blend({"Crude3"})({"Regular"})" index="5" status="BS" value="3166.66666666667" reducedCost="-0"/> <variable name="Blend({"Crude1"})({"Diesel"})" index="6" status="BS" value="666.666666666667" reducedCost="-0"/> <variable name="Blend({"Crude2"})({"Diesel"})" index="7" status="BS" value="333.333333333333" reducedCost="-0"/> <variable name="Blend({"Crude3"})({"Diesel"})" index="8" status="LL" value="0" reducedCost="-7.105427357601e-15"/> <variable name="Inventory({"Super"})" index="9" status="LL" value="0" reducedCost="-20.9"/> <variable name="Inventory({"Regular"})" index="10" status="BS" value="7500" reducedCost="-0"/> <variable name="Inventory({"Diesel"})" index="11" status="LL" value="0" reducedCost="-40.9"/> <variable name="x13" index="12" status="UL" value="0" reducedCost="1"/> </variables> </CPLEXSolution>' x <- read_xml(xml_file) # Get every <variable> element inside the <variables> node vars <- xml_find_all(x, "//variables/variable") # Extract attributes and convert to usable data types variable_df <- data.frame( Index = as.integer(xml_attr(vars, "index")), Name = xml_attr(vars, "name"), Value = as.numeric(xml_attr(vars, "value")), ReducedCost = as.numeric(xml_attr(vars, "reducedCost")), # Optional, if you need this metric stringsAsFactors = FALSE ) # View the cleaned-up results print(variable_df)
Key Explanations
- Targeting individual variables: Instead of just selecting the parent
<variables>node, we use the XPath//variables/variableto grab every child<variable>element. - Extracting attributes: The
xml_attr()function pulls the value of a specified attribute from each element in thevarslist. We convertindexto integer and numeric fields likevalueto numeric so they’re ready for analysis. - Data frame organization: This structure makes it easy to filter, sort, or export the variable data for further work.
内容的提问来源于stack exchange,提问作者rwdvc
相关产品推荐
相关产品推荐

