简体   繁体   中英

How should I parse XML-like data in JavaScript?

I have data sets similar to this:

<NDL>
<REPLICA 4925770B:0025BA85>
<VIEW OF64623968:A2336DB0-ON49256C46:002ACF42>
<NOTE OFA52D3E8C:0ED3F84A-ON605F586A:5D1C1FAA>
<HINT>CN=YW8LN6/O=TDK-JP</HINT>
<REM>Database 'Shunya Sato', View '受信ボックス', Document '[Requirement management system - Feature #125] (New) Collect example of LN link'</REM>
</NDL>

I need to retrieve the content enclosed by the <HINT> tag, and the pseudo-attributes in the , and tags. Is there some lib that could help me out with this, or is the best way to hope that everything will always be in this order and use split/find/other builtin stuff?

Unfortunately, unless you write a custom parser that can turn what you have into XML, you won't be able to use any traditional XML libraries to read your data. The only reason that people can perform XML queries over HTML is because there are clearly defined ways to convert HTML into a DOM, which can then be converted into XML. The same cannot be said for your data.

While your data may resemble XML, the only thing it has in common is the use of < and > to delimit fields. As such, you are probably just better off using string searching and spliting to get the fields you need.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM