9 Replies Latest reply: Mar 22, 2012 1:16 AM by 925315 RSS

    Using Regular Expression in XML parsing

    925315
      Hi

      I have an XML File with the node names like "ns670:PTC_FareBreakdown" etc. Now i need to get to the "PTC_FareBreakdown" node but the "ns670:" expression is not static and keeps changing dynamically. Is there any way to get the element by the tag name "PTC_FareBreakdown" using some regular expression in Java using a DOM Parser? Its really urgent.

      <PTC_FareBreakdowns SequenceNo="1">
      <ns670:PTC_FareBreakdown>...</ns670:PTC_FareBreakdown>
      <ns690:PTC_FareBreakdown>...</ns690:PTC_FareBreakdown>
      <ns890:PTC_FareBreakdown>...</ns890:PTC_FareBreakdown>
      </PTC_FareBreakdowns>

      Thanks & Regards

      Tarni Sharma
        • 1. Re: Using Regular Expression in XML parsing
          EJP
          the "ns670:" expression is not static and keeps changing dynamically
          Why? This indicates a problem upstream. Fix that. Don't just build in co-operating bugs.
          • 2. Re: Using Regular Expression in XML parsing
            925315
            I know that but if that could be done i would have done that already. But that is not to be touched. SO I am still stuck with my problem.
            • 3. Re: Using Regular Expression in XML parsing
              EJP
              Your problem was created by someone else. Let them solve it. On their budget. Tell your boss.
              • 4. Re: Using Regular Expression in XML parsing
                925315
                What you are saying is right and I already talked to the boss.
                It still is my problem.
                • 5. Re: Using Regular Expression in XML parsing
                  gimbal2
                  user3591878 wrote:
                  What you are saying is right and I already talked to the boss.
                  It still is my problem.
                  These are the moments where paranoia is your friend. Get a confirmation through email first before you do anything else. Get proof that you indicated the problem and that someone else made the wrong decision. Just in case.

                  The "solution" does not have to be regex. You know the simplistic rule: the node name must END WITH a certain String which is static. However the data is loaded from the DOM tree must be adapted to be able to apply that logic.
                  • 6. Re: Using Regular Expression in XML parsing
                    EJP
                    What you are saying is right and I already talked to the boss.
                    It still is my problem.
                    Just make sure he knows he is spending money on someone else's problem.
                    • 7. Re: Using Regular Expression in XML parsing
                      jtahlborn
                      That "ns670" string is an xml namespace prefix. it should have a corresponding xml namespace declaration somewhere in the xml document (i'm guessing you have not shown the whole document). the actual value of an xml namespace prefix is meaningless. if you parse the xml with a namespace aware DOM parser, it should generate Nodes with the correct namespace. the namespace is the value you care about when extracting data from the document, not the namespace prefix.

                      alternately, if you parse the document using a namespace aware DOM parser, you can just look for nodes based on their "local" name (the part after the ":" separator) and ignore the namespace/prefix.

                      whatever you do, please do not parse the xml with a regex, see this http://stackoverflow.com/a/1732454/552759 for details (applies to xml as well).
                      • 8. Re: Using Regular Expression in XML parsing
                        DrClap
                        EJP wrote:
                        What you are saying is right and I already talked to the boss.
                        It still is my problem.
                        Just make sure he knows he is spending money on someone else's problem.
                        We do that all the time where I work. It gets filed under the heading of "customer service" but we still end up with a code base full of workarounds and cheap hacks.
                        • 9. Re: Using Regular Expression in XML parsing
                          925315
                          @jtahlborn: Thanks for the reply. I think this should work
                          @DrClap: I know that feeling..Its probably what 70% of Software Engineers do. Do Patches because of someone else's faults !