[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 282
  • Last Modified:

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
0
ITtelligent
Asked:
ITtelligent
  • 5
  • 2
1 Solution
 
Geert BormansCommented:
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
0
 
Geert BormansCommented:
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
0
 
Bill PrewCommented:
Where does it need to go in the destination XML file, is there a node structure that it needs to fit underneath, etc?

~bp
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
Geert BormansCommented:
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
0
 
Bill PrewCommented:
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
0
 
Geert BormansCommented:
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
0
 
Geert BormansCommented:
That doesn't mean it has to be XSLT.
You can do some XML DOM processing in Python, Ruby, Perl, .... and make that a batch program
That implies installing the programming language at hand, plus an XML library.... it is about as bad as downloading an XSLT processor... it is a bit depending upon taste
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

  • 5
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now