How to change a value in a XML-File?

조회 수: 16 (최근 30일)
Moritz
Moritz 2013년 10월 2일
댓글: Robert Ungi 2022년 1월 3일
Hello,
i have this XML-code
<busStop id="BusStop0" lane="1to2_0" startPos="20" endPos="30"/>
<busStop id="BusStop1" lane="1to2_0" startPos="70" endPos="80"/>
<chargingStation id="ChrgStn1" lane="1to2_0" startPos="20" endPos="30" chrgPower="220"/>
I want to change the value in "chargingStation" for "startPos" and for "endPos".
How do i write in MatLab?
Thanks!!!

답변 (1개)

Alessandro
Alessandro 2013년 10월 2일
편집: Alessandro 2013년 10월 2일
If I understand you right and you use the java node structure for matlab than you could use this function to replace the node:
%Function to replace a w3c node
function xmlreplacenode(node1,nodenew)
parent = node1.getParentNode;
node1.getParentNode.removeChild(xmlnode);%Delete the node
parent.appendChild(nodenew);
If not you should first parse the document with:
xDoc = xmlread(handles.tmpverzeichn);
You still need a function to generate a new node from a string maybe. For that you can try this:
%input doc is java org.w3c.dom Document
%str is a xml string
function node = parsestr2node(doc,str)
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.DocumentBuilder;
import org.xml.sax.SAXException;
import org.xml.sax.SAXParseException;
import org.w3c.dom.Node;
import java.io.*;
inStream = org.xml.sax.InputSource();
inStream.setCharacterStream(java.io.StringReader(str));
docBuilderFactory = DocumentBuilderFactory.newInstance();
docBuilder = docBuilderFactory.newDocumentBuilder();
tmpdoc = docBuilder.parse (inStream);
node = doc.importNode(tmpdoc.getChildNodes.item(0),true);
If it doesn t work maybe you need some java files
  댓글 수: 4
Heinke W
Heinke W 2021년 11월 4일
편집: Heinke W 2021년 11월 4일
Hi Alessandro,
first of all I want to say thank you for your reply. I got a similar issue. I have to admit to be noob, especially when it comes to XML-syntax in matlab. That’s why I need a bit more help than others.
I have the following XML-file which is located in the folder c:\Data\Testfile.xml
<?xml version='1.0' encoding='UTF-8'?>
<tns:testBenchConfiguration>
<tns:instrumentList/>
<tns:VariableList>
<tns:Variable>
<tns:value>1</tns:value>
<tns:format>double</tns:format>
<tns:description/>
<tns:tag/>
<tns:path>Model Root/general/Value</tns:path>
<tns:guiName>approval</tns:guiName>
</tns:Variable>
</tns:VariableList>
</tns:testBenchConfiguration>
Now I want to open this XML file with matlab, change the value from "1" to "2" and save the change afterwards.
I use matlab 2015, if this is important
Robert Ungi
Robert Ungi 2022년 1월 3일
@Heinke W this might help:
% <?xml version="1.0" encoding="utf-8"?>
% <xml>
% <busStop endPos="30" id="BusStop0" lane="1to2_0" startPos="20"/>
% <busStop endPos="80" id="BusStop1" lane="1to2_0" startPos="70"/>
% <chargingStation chrgPower="220" endPos="30" id="ChrgStn1" lane="1to2_0" startPos="45"/>
% </xml>
import javax.xml.xpath.*
factory = XPathFactory.newInstance;
import javax.script.ScriptContext;
xpath = factory.newXPath;
fXML=xmlread('SampleXML.xml');
expression = xpath.compile('xml/chargingStation[@id="ChrgStn1"]');
chargingStation = expression.evaluate(fXML, XPathConstants.NODESET);
for i=0:chargingStation.getLength-1
chargingStation.item(i).setAttribute('startPos', "45")
end
xmlwrite('SampleXMLOut.xml', fXML);
Adopted from The Wizzart.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Structured Data and XML Documents에 대해 자세히 알아보기

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by