issue with reading xml file!
26 次查看(过去 30 天)
显示 更早的评论
Dear all,
i tried to read an xml file using xmlread but got a long error msg:
Error using xmlread (line 116)
Java exception occurred:
org.xml.sax.SAXParseException; systemId:
file:/C:/All%20Matlab%20Data%20Analysis%202023/convert%20xml%20to%20mat/Example_xmlfile.xml; lineNumber: 29;
columnNumber: 3; The markup in the document following the root element must be well-formed
my xml formt is attached (As text format as xml is not support for uoloading).
appreciated output is:
Main1 eFun1 a1 A1A
Main1 eFun1 a2 0
Main1 eFun1 a3 3
Main1 eFun1 a4 B1B
Main1 eFun2 b1 1
Main1 eFun2 b2 1
Main1 eFun2 b3 0
Main1 eFun3 c1 202
Main1 eFun3 c2 -13
Main1 eFun3 c3 4
Main2 bFun1 x1 Ngb
Main2 bFun1 x2 0
Main2 bFun1 x3 4
Main2 bFun1 x4 a0a
Main2 bFun2 y1 0
Main2 bFun2 y2 cnc
Main2 bFun2 y3 4
Main2 bFun2 y4 69
Thank you,
Br.
Joseph
0 个评论
回答(2 个)
Harsh Mahalwar
2023-5-26
编辑:Harsh Mahalwar
2023-5-26
Hi Joseph,
You can use the parseXML function given in the following documentation to read your xml file.
Just remember to change the tag names accordingly!
Thanks and regards,
Harsh Mahalwar
Kevin Gurney
2023-5-26
编辑:Kevin Gurney
2023-5-26
Hi Joseph,
The xmlread parsing error you are encountering appears to be related to the fact that you have more than one root-level node in your XML document (specifically, there are two spec:syndata nodes at the the top-level).
An easy way to verify that this is causing the issue is to add a new root node to the document that wraps all the other nodes (I've called this node <root> for simplicity in the example below).
<root>
<spec:syndata FunctionType="Main2" xmlns:spec="foo">
<class>
<eFun1>
<attributes>
<a1>A1A</a1>
<a2>0</a2>
<a3>3</a3>
<a4>B1B</a4>
</attributes>
</eFun1>
</class>
<class>
<eFun2>
<attributes>
<b1>1</b1>
<b2>1</b2>
<b3>0</b3>
</attributes>
</eFun2>
<eFun3>
<attributes>
<c1>202</c1>
<c2>1</c2>
<c3>-13</c3>
</attributes>
</eFun3>
</class>
</spec:syndata>
<spec:syndata FunctionType="Main2">
<class>
<bFun1>
<attributes>
<x1>Ngb</x1>
<x2>0</x2>
<x3>4</x3>
<x4>a0a</x4>
</attributes>
</bFun1>
</class>
<class>
<bFun2>
<attributes>
<y1>0</y1>
<y2>cnc</y2>
<y3>4</y3>
<y4>69</y4>
</attributes>
</bFun2>
</class>
</spec:syndata>
</root>
With this change, you should be able to read this file using xmlread without any issues.
Best Regards,
Kevin Gurney
4 个评论
Tatjana
2023-9-19
Hi Stephen23,
could you possibly explain, why one needs to enter "....item(0)" twice in that example? I tried this example and an example of my own that's similar and you are right (though the method does not really work with this example: https://de.mathworks.com/help/matlab/ref/matlab.io.xml.dom.nodelist-class.html) So when I enter "....item(0)" the first time with "two = one.item(0)" where are we exactly? Inside <a1> but not in between <a1>...<a1>?
My second question refers to the children nodes. Which command gives you the listing of all <attributes>-belongings such as <a1>A1A</a1>, <a2>0</a2> <a3>3</a3> <a4>B1B</a4>?
Thanks
Stephen23
2023-9-20
"where are we exactly? Inside <a1> but not in between <a1>...<a1>? "
As far as I can tell the first ITEM call selects which node, whereas the second ITEM call refers to the content of the node. Clearly there could be multiple nodes with the same name so we need to unambiguously select which one we want to refer to, and then any single node can have multiple content (text and other nodes) so once again we need to select which one we want to refer to.
Here I modified the file so that it contains two "a1" nodes:
dom = xmlread('Modified_xmlfile.txt');
one = dom.getElementsByTagName('a1');
one.item(0).item(0)
one.item(1).item(0)
"though the method does not really work with this example..."
I would not expect a different tool to work in the same way.
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 String Parsing 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!