0

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?

8
  • Check stackoverflow.com/questions/12032562/… Commented Jul 16, 2013 at 23:25
  • 2
    The problem is that this is not well-formed XML. Commented Jul 16, 2013 at 23:26
  • @elclanrs That won't work as this isn't XML. Commented Jul 16, 2013 at 23:39
  • HTML is great at parsing code without tags like meta and img: t=document.createElement("div"); t.innerHTML=(your code); alert(t.getElementsByTagName("hint")[0].textContent); which works... Commented Jul 16, 2013 at 23:48
  • 1
    I guess they've probably got an excuse in that they did it in the dark ages before XML was invented. If so, they are forgiven. Commented Jul 18, 2013 at 7:26

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

2 Comments

On a related note, Python has a very useful sequance unpacking feature, as so: firstLine, secondLine = data.split("\n", 1). Is there an equivalent to this in JavaScript?
@Glycan: The split function has, the destructuring assignment is not supported everywhere and would look a bit different.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.