Link to home
Start Free TrialLog in
Avatar of ITtelligent
ITtelligent

asked on

Extract XML Line to a new file

I have an XML file and I need one line of this XML file to be extracted to another XML file. Within the XML file the line I need extracted is the following:

<ClientCount>5</ClientCount>

Can you advise how i go about doing this, batch is fine
Avatar of Gertone (Geert Bormans)
Gertone (Geert Bormans)
Flag of Belgium image

you can easily do that with an XSLT

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    version="1.0">
    <xsl:template match="/">
        <xsl:copy-of select="//ClientCount"/>
    </xsl:template>
</xsl:stylesheet>

Open in new window


Paste that code in a file
and run that file with an XSLT processor against your source XML
download saxon home edition
http://sourceforge.net/projects/saxon/files/Saxon-HE/9.4/SaxonHE9-4-0-4J.zip/download
(make sure you have java installed)
put the jar files somewhere on your disk

here is how to run then

java -cp %saxon% net.sf.saxon.Transform %ifile% -xsl:%xsl% -o:%ofile%

%saxon% = filename + path of the saxonhe jar
%xsl% = filename + path opf the XSLT
%ifile% = the filename + path of your input file
%ofile% = the filename + path of your output file
Avatar of Bill Prew
Bill Prew

Where does it need to go in the destination XML file, is there a node structure that it needs to fit underneath, etc?

~bp
yeah well, that is an interesting question.
I assumed the OP needed a new XML with just that one line.

It is pretty easy to take an XML, cut that line and paste it in another XML,
using the code above (slightly changed)
So if the requirement is to change an existing XML with this line, let me know and I change the code
Well, if you just want to extract that single line to a new XML file, that's pretty easy with the following DOS command, either at the command prompt, or in a BAT file:

find /i "<ClientCount>" <in.xml >out.xml

Open in new window

~bp
I strongly disagree.
It is a very common mistake to create XML not using a parsing XML application.

Your DOS command suggestion can go wrong for many reasons
- Your source XML can have an encoding other than UTF-8, by not copying the encoding with the snippet, you could get character encoding conflicts
- there could be a doctype with entities that would not be resolved, leaving the XML unwellformed
- the ClientCount is not necesarily on one line and isolated

Even if all conditions are safe now, the XML provider can change its setup at any point breaking thsi application without notice

XML processing with regex tools is only safe in a very closed environment.
I have to deal with crap XML crafted with regexes on a regular basis, please don't teach people to get down that way
ASKER CERTIFIED SOLUTION
Avatar of Gertone (Geert Bormans)
Gertone (Geert Bormans)
Flag of Belgium image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial