Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > XML > XML
Password Reminder
Register
| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
XML General XML discussions.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the XML section of the Wrox Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Search this Thread Display Modes
  #11 (permalink)  
Old December 29th, 2003, 02:13 AM
Friend of Wrox
 
Join Date: Jun 2003
Location: Nor Hachin, Kotayk, Armenia.
Posts: 147
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via Yahoo to armmarti
Default

Quote:
quote:Originally posted by AyatKh
 Hi Armen,
Thank you very much for your great response. I think this is the best time to ask your advice for my project.

Right now our users are dealing with downloading more than 200 word2002 files from our application. what it does is it search for some text fields in the word file and replace those fields with related data from Database. The problem is it's so slow. So we want to convert those file into Word2003 and save them as XML file so that the search and replace would be easier. Do you think using XML and XSLT for doing this search and replace is the best solution?
Thanks
Ayat

Hi Ayat,

Really I think that XSLT solution is convenient (from my point of view). It may be slow since the algorithm is designed to walk through the whole document and also it's recursive. I just can suggest to use XSLTC from Apache. It's free and it's really fast (though it seems that there are some bugs and weak points yet). XSLTC is an XSLT compiler and it first compiles the stylesheet (creates a java .class file) and uses that file to perform a transformation. It will enhance the speed for sure.



****************************
I wish all the people of this forum all the best in 2004, especially peace and health!
Happy New Year!
****************************


Best regards,
Armen Martirosyan
Reply With Quote
  #12 (permalink)  
Old December 29th, 2003, 10:07 AM
Authorized User
 
Join Date: Dec 2003
Location: , , USA.
Posts: 26
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via Yahoo to AyatKh
Default

Hi Armen,

Thanks for your reply. We are using .NET so that I have to find a solution using vb.net. But here is another question for you. Is there any way to save an XML file into string. because I can use the String.Replace for search and replace a text in XML file.
Thanks
Ayat

Reply With Quote
  #13 (permalink)  
Old December 30th, 2003, 02:00 AM
Friend of Wrox
 
Join Date: Jun 2003
Location: Nor Hachin, Kotayk, Armenia.
Posts: 147
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via Yahoo to armmarti
Default

Quote:
quote:Originally posted by AyatKh
 Hi Armen,

Thanks for your reply. We are using .NET so that I have to find a solution using vb.net. But here is another question for you. Is there any way to save an XML file into string. because I can use the String.Replace for search and replace a text in XML file.
Thanks
Ayat

Sorry Ayat, but saving XML document as string and making replacements is against the paradigm of XML... An XML document can't be viewed as a sequence of '<', '>', '/' and other "ordinary" characters. Your suggestion will be failed when you'll attempt to replace the string "abc" whith "def" having element type(s) "*abc*" or attribute(s) named "*abc*".
So I can suggest to parse the XML document, get the DOM tree and walk over the tree and make replacements only for text nodes(you can compare XSLT and DOM solutions and choose the best one).
If the speed is critical for you, I'm sure that the correctness is much more important.

Regards,
Armen
Reply With Quote
  #14 (permalink)  
Old December 30th, 2003, 10:40 AM
Authorized User
 
Join Date: Dec 2003
Location: , , USA.
Posts: 26
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via Yahoo to AyatKh
Default

Hi Armen,
Thank you very much for your reply. I just want to make sure that my idea doesn't work. So based on what you said, I can't open up an XML file(a word2003 file saved as an xml file) and do search and replace using fileStream or dataset and then save it and at the end open it up with word2003 again. right?
Like I have a word2003 file which I have saved it as an XML file. "This is a test" I want to replace "test" with "test1" and I want to have a output with "This is a test1", which is also an XML file.
So If there is no way of doing that, could you show me how to write it with DOM and walk through the document.
I would be thankful.
Ayat

Reply With Quote
  #15 (permalink)  
Old June 27th, 2006, 08:28 AM
Registered User
 
Join Date: Jun 2006
Location: Verona, Verona, Italy.
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default


I need to replace "&amp;apos;" by "&apos;" in an attribute value of an element. I tried with the code below but does nothing. I wonder why. Can somebody help me?

the xml file:

 <dest id_nsis=" " rag_soc="CANTU&amp;apos; MARIA PIA" titolare="" >
  <id_cli>1000346</id_cli>
 </dest>


I would like to have:
<dest id_nsis=" " rag_soc="CANTU&apos; MARIA PIA" titolare="" >
  <id_cli>1000346</id_cli>
 </dest>

Quote:
quote:Originally posted by armmarti
 
Quote:
quote:Originally posted by AyatKh
Quote:
 For example I need to replace "Barnes And Noble" with "Test" in this code. what is the best way to search for that text? Which object I should use?

<book genre="novel" ISBN="1-861001-57-5">
<title>Pride And Prejudice</title>
<seller>Barnes And Noble</seller>
<price>$45</price>
</book>

You can achieve this by processing the DOM tree(by parsing the document using TrAX and then manipulate the DOM tree using Java, for example). Another approach is a stylesheet:

Code:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:param name="what" select="'Barnes And Noble'"/>
    <xsl:param name="by-what" select="'Test'"/>
    <xsl:template match="/">
        <xsl:apply-templates/>
    </xsl:template>

    <xsl:template match="node() | @*">
        <xsl:choose>
            <xsl:when test="self::text()">
                <xsl:variable name="this-text" select="self::text()"/>
                <xsl:choose>
                    <xsl:when test="contains($this-text, $what)">
                        <xsl:call-template name="replace">
                            <xsl:with-param name="str" select="$this-text"/>
                            <xsl:with-param name="what" select="$what"/>
                            <xsl:with-param name="by-what" select="$by-what"/>
                        </xsl:call-template>
                    </xsl:when>
                    <xsl:otherwise>
                        <xsl:copy-of select="."/>
                    </xsl:otherwise>
                </xsl:choose>
            </xsl:when>
            <xsl:otherwise>
                <xsl:copy>
                    <xsl:apply-templates select="node() | @*"/>
                </xsl:copy>
            </xsl:otherwise>
        </xsl:choose>
    </xsl:template>

    <xsl:template name="replace">
        <xsl:param name="str"/>
        <xsl:param name="what"/>
        <xsl:param name="by-what"/>

        <xsl:choose>
            <xsl:when test="not($str)"/>
            <xsl:when test="starts-with($str, $what)">
                <xsl:value-of select="$by-what"/>
                <xsl:call-template name="replace">
                    <xsl:with-param name="str" select="substring($str, string-length($what)+1)"/>
                    <xsl:with-param name="what" select="$what"/>
                    <xsl:with-param name="by-what" select="$by-what"/>
                </xsl:call-template>
            </xsl:when>
            <xsl:otherwise>
                <xsl:value-of select="substring($str, 1, 1)"/>
                <xsl:call-template name="replace">
                    <xsl:with-param name="str" select="substring($str, 2)"/>
                    <xsl:with-param name="what" select="$what"/>
                    <xsl:with-param name="by-what" select="$by-what"/>                
                </xsl:call-template>    
            </xsl:otherwise>
        </xsl:choose>
    </xsl:template>
</xsl:stylesheet>
This stylesheet replaces all occurences of string kept in variable $what with a string kept in variable $by-what in all text nodes.

Regards,
Armen


Reply With Quote
  #16 (permalink)  
Old June 27th, 2006, 11:50 AM
mhkay's Avatar
Wrox Author
Points: 18,487, Level: 59
Points: 18,487, Level: 59 Points: 18,487, Level: 59 Points: 18,487, Level: 59
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Apr 2004
Location: Reading, Berks, United Kingdom.
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

The code you've shown is fine. How are you invoking it?

Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference
Reply With Quote
  #17 (permalink)  
Old June 28th, 2006, 01:46 AM
Registered User
 
Join Date: Jun 2006
Location: Verona, Verona, Italy.
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default

I've tried to do it with .NEt and then XSLTrasform method

'XSLT to trasform
Dim sXSL As String = "MyXSLTFile.xslt"

Dim xmldoc As XmlDataDocument = New XmlDataDocument
xmldoc.Load(sPath & FileName & ".xml")

Dim xslTras As XslTransform = New XslTransform
xslTras.Load(sPath & "/" & sXSL)

'trasform and save
Dim TWrtr As New XmlTextWriter(sPath & "\NewXMLFile.xml", System.Text.Encoding.Default)
xslTras.Transform(xmldoc, Nothing, TWrtr, Nothing)
TWrtr.Close()

I have to modify an attribute value.
Thanks for any help.

Reply With Quote
  #18 (permalink)  
Old May 23rd, 2012, 09:05 PM
Authorized User
Points: 98, Level: 1
Points: 98, Level: 1 Points: 98, Level: 1 Points: 98, Level: 1
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Dec 2011
Posts: 23
Thanks: 3
Thanked 0 Times in 0 Posts
Default A little late but here is a simple fix:

using System.IO;
using System.Text.RegularExpressions;
{
TextReader tr = File.OpenText("../../XmlFile1.xml");
string oldString = tr.ReadToEnd();
tr.Close();


string pattern= @"(Missy)";
string newString= Regex.Replace(oldString,pattern,"Miles");

TextWriter tw = File.CreateText("../../Output.xml");
tw.WriteLine(newString);
tw.Close();
}

XmlFile1.xml :

<?xml version="1.0" encoding="utf-8" ?>
<Pigs>
<pig name="Missy">
<weight>72</weight>
<height>22</height>
<girth>100</girth>
<food>corn</food>
</pig>
<pig name="Sissy">
<weight>472</weight>
<height>32</height>
<girth>300</girth>
<food>wheat</food>
</pig>
<pig name="Ditsy">
<weight>788</weight>
<height>10</height>
<girth>1000</girth>
<food>trash</food>
</pig>
</Pigs>
Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
HELP - Find text / Replace with email link (XML or peakfx XML 1 July 13th, 2007 09:07 PM
XSL to replace text in XML file neetukk XSLT 1 November 29th, 2006 05:20 AM
Global Search&Replace Text in all fields in sql db buddyz SQL Server 2000 10 September 14th, 2006 08:24 AM
Binary Search on a text file scoobie Pro Java 1 August 25th, 2006 12:43 AM
How to replace elements(from existed XML file)? hbcontract XML 1 October 30th, 2003 05:49 AM



All times are GMT -4. The time now is 10:28 AM.


Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.