Regular views

The content of the root element needs to be captured <pubDate>

, but in a document it can be either inside the element <item>

or inside the element <channel>

. Also <item>

is a child <channel>

I will give an example

<channel>
  ...
  <pubDate>10/2/2010</pubDate>
  ...
  <item>
    ...
    <pubDate>13/2/2029</pubDate>
    ...
  </item>
  ...
</channel>

      

must be fixed on 10/2/2010

If the problem <item>

cannot be resolved, it can be recorded along with it <pubDate>

.

+2


a source to share


3 answers


I don't know JavaScript, so I can't help you with the DOM. I agree 100% that it is a bad idea to try and parse XML with regex. However, there can be a quick, very messy and very fragile workaround:

If the indentation is consistent throughout the file, and the elements are <channel>

always at the same indentation level, you can use that fact as a guide to the regexp. In your example /^ {2}<pubDate>([^<]*)<\/pubdate>/m

(= two spaces after the start of the line) might just work.



Use this at your own risk. There will be dragons, etc.

+1


a source


Regexp is not a good tool for working with a programming language that is parsed with context-free grammars. Try using XML DOM to get the job done.



+2


a source


Check out jQuery and see if it helps reading / parsing XML: http://think2loud.com/reading-xml-with-jquery/

KM

+1


a source







All Articles